Xml 如何基于复杂子元素上的条件映射复杂元素列表';使用xslt创建属性
我有一个结构如下的xml文件,其中包含许多重复的元素 更新:在输入xml中,仅提供那些可能导致映射更改的元素Xml 如何基于复杂子元素上的条件映射复杂元素列表';使用xslt创建属性,xml,xslt,attributes,parent-child,Xml,Xslt,Attributes,Parent Child,我有一个结构如下的xml文件,其中包含许多重复的元素 更新:在输入xml中,仅提供那些可能导致映射更改的元素 <offerList> <!--1 or more repetitions:--> <offer> <!--1 or more repetitions:--> <productList minQuantity="0" maxQuantity="1"> <list
<offerList>
<!--1 or more repetitions:-->
<offer>
<!--1 or more repetitions:-->
<productList minQuantity="0" maxQuantity="1">
<listType>1</listType>
<!--1 or more repetitions:-->
<productCategory minQuantity="0" maxQuantity="1">
<controlType>11</controlType>
<product minQuantity="0" maxQuantity="1">
<productKey>111</productKey>
<!--1 or more repetitions:-->
<productOption disable="?" rulePicked="false" picked="true" defaultSelection="?" requireValidation="?">
<optionKeyType>1111</optionKeyType>
</productOption>
<productOption disable="?" rulePicked="true" picked="true" defaultSelection="?" requireValidation="?">
<optionKeyType>1112</optionKeyType>
</productOption>
</product>
<product minQuantity="0" maxQuantity="1">
<productKey>112</productKey>
<!--1 or more repetitions:-->
<productOption disable="?" rulePicked="true" picked="true" defaultSelection="?" requireValidation="?">
<optionKeyType>1121</optionKeyType>
</productOption>
<productOption disable="?" rulePicked="false" picked="true" defaultSelection="?" requireValidation="?">
<optionKeyType>1122</optionKeyType>
</productOption>
<productOption disable="?" rulePicked="true" picked="false" defaultSelection="?" requireValidation="?">
<optionKeyType>1123</optionKeyType>
</productOption>
<productOption disable="?" rulePicked="false" picked="false" defaultSelection="?" requireValidation="?">
<optionKeyType>1124</optionKeyType>
</productOption>
</product>
</productCategory>
<productCategory minQuantity="0" maxQuantity="1">
<controlType>12</controlType>
<!--1 or more repetitions:-->
<product minQuantity="0" maxQuantity="1">
<productKey>121</productKey>
<!--1 or more repetitions:-->
<productOption disable="?" rulePicked="false" picked="true" defaultSelection="?" requireValidation="?">
<optionKeyType>1211</optionKeyType>
</productOption>
<productOption disable="?" rulePicked="false" picked="true" defaultSelection="?" requireValidation="?">
<optionKeyType>1212</optionKeyType>
</productOption>
</product>
</productCategory>
<productCategory minQuantity="0" maxQuantity="1">
<controlType>13</controlType>
<!--1 or more repetitions:-->
<product minQuantity="0" maxQuantity="1">
<productKey>131</productKey>
<!--1 or more repetitions:-->
<productOption disable="?" rulePicked="true" picked="true" defaultSelection="?" requireValidation="?">
<optionKeyType>1311</optionKeyType>
</productOption>
<productOption disable="?" rulePicked="false" picked="true" defaultSelection="?" requireValidation="?">
<optionKeyType>1312</optionKeyType>
</productOption>
</product>
</productCategory>
</productList>
</offer>
</offerList>
使用Xpath
offerList/offer/productList/productCategory/product/productOption
条件是
either rulePicked is true or picked is false
我尝试创建的XSLT是:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name="ListOfCategory">
<xsl:for-each select="offerList/offer">
<xsl:for-each select="./productList">
<xsl:for-each select="./productCategory">
<xsl:for-each select="./product">
<xsl:for-each select="./productOption">
<xsl:if test="(./@rulePicked = true) or (./@picked = false)">
<xsl:copy-of select="../../../"/>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:variable>
</xsl:template>
</xsl:stylesheet>
此XSLT为满足条件的每个productOption创建一个productCategory元素
但是我想要productCategory元素,其中包含所有满足条件的productOptions。并且必须删除该产品类别中的所有其他产品操作
对于我给出的输入,我想要的输出应该是这样的:
<offerList>
<!--1 or more repetitions:-->
<offer>
<!--1 or more repetitions:-->
<productList minQuantity="0" maxQuantity="1">
<listType>1</listType>
<!--1 or more repetitions:-->
<productCategory minQuantity="0" maxQuantity="1">
<controlType>11</controlType>
<product minQuantity="0" maxQuantity="1">
<productKey>111</productKey>
<!--1 or more repetitions:-->
<productOption disable="?" rulePicked="true" picked="true" defaultSelection="?" requireValidation="?">
<optionKeyType>1112</optionKeyType>
</productOption>
</product>
<product minQuantity="0" maxQuantity="1">
<productKey>112</productKey>
<!--1 or more repetitions:-->
<productOption disable="?" rulePicked="true" picked="true" defaultSelection="?" requireValidation="?">
<optionKeyType>1121</optionKeyType>
</productOption>
<productOption disable="?" rulePicked="true" picked="false" defaultSelection="?" requireValidation="?">
<optionKeyType>1123</optionKeyType>
</productOption>
<productOption disable="?" rulePicked="false" picked="false" defaultSelection="?" requireValidation="?">
<optionKeyType>1124</optionKeyType>
</productOption>
</product>
</productCategory>
<productCategory minQuantity="0" maxQuantity="1">
<controlType>13</controlType>
<!--1 or more repetitions:-->
<product minQuantity="0" maxQuantity="1">
<productKey>131</productKey>
<!--1 or more repetitions:-->
<productOption disable="?" rulePicked="true" picked="true" defaultSelection="?" requireValidation="?">
<optionKeyType>1311</optionKeyType>
</productOption>
</product>
</productCategory>
</productList>
</offer>
</offerList>
1.
11
111
1112
112
1121
1123
1124
13
131
1311
谁能帮我弄到这个
提前感谢针对澄清编辑:
试着这样做:
XSLT1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="offer[not(productList/productCategory/product/productOption[@rulePicked='true' or @picked='false'])]"/>
<xsl:template match="productList[not(productCategory/product/productOption[@rulePicked='true' or @picked='false'])]"/>
<xsl:template match="productCategory[not(product/productOption[@rulePicked='true' or @picked='false'])]"/>
<xsl:template match="product[not(productOption[@rulePicked='true' or @picked='false'])]"/>
<xsl:template match="productOption[not(@rulePicked='true' or @picked='false')]"/>
</xsl:stylesheet>
这将按原样复制输入中的所有内容,但以下内容除外:
- 任何不满足条件的
productOption
- 任何
,其中至少没有一个产品
子项满足该条件productOption
- 任何
,其中至少没有一个productCategory
子体满足该条件productOption
- 任何
,其中至少没有一个productList
子体满足该条件productOption
- 没有至少一个满足条件的
后代的任何productOption
提供
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="offer[not(productList/productCategory/product/productOption[@rulePicked='true' or @picked='false'])]"/>
<xsl:template match="productList[not(productCategory/product/productOption[@rulePicked='true' or @picked='false'])]"/>
<xsl:template match="productCategory[not(product/productOption[@rulePicked='true' or @picked='false'])]"/>
<xsl:template match="product[not(productOption[@rulePicked='true' or @picked='false'])]"/>
<xsl:template match="productOption[not(@rulePicked='true' or @picked='false')]"/>
</xsl:stylesheet>