Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 如何基于复杂子元素上的条件映射复杂元素列表';使用xslt创建属性_Xml_Xslt_Attributes_Parent Child - Fatal编程技术网

Xml 如何基于复杂子元素上的条件映射复杂元素列表';使用xslt创建属性

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

我有一个结构如下的xml文件,其中包含许多重复的元素

更新:在输入xml中,仅提供那些可能导致映射更改的元素

    <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
    后代的任何
    提供


“但我希望productCategory元素中的所有productOptions都满足条件。必须删除该productCategory中的所有其他ProductOprion。”IMHO,输入和预期输出的示例有助于理解这一点。我可以在这里进一步处理输出吗?例如,在单个offer元素上循环并将其映射到不同的结构?“我可以在这里进一步处理输出吗?”是的,可以。只需添加一个与要处理的元素匹配的模板,并写出预期的输出。-顺便说一句,从循环的角度考虑是没有用的。
<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>