如何使用xslt获取父节点内的子节点

时间:2018-04-19 00:59:55

标签: xslt xslt-2.0

我有一个格式为

的xml
<ModelAttrib>
    <ModelAttribData>
        <ID>5</ID>
        <AttributeID>1</AttributeID>
    </ModelAttribData>
    <Attribute>
        <AttributeData>
            <ID>1</ID>
            <AttribVariation>
                <AttribVariationData>
                    <ID>2</ID>
                    <AttributeID>1</AttributeID>
                    <ModelAttribVar>
                        <ModelAttribVarData>
                            <ID>3</ID>
                            <AttribVariationID>2</AttribVariation>
                        </ModelAttribVarData>
                    </ModelAttribVar>   
                </AttribVariationData>
            </AttribVariation>
            <AttribVariation>
                <AttribVariationData>
                    <ID>9</ID>
                    <AttributeID>1</AttributeID>
                    <ModelAttribVar>
                        <ModelAttribVarData>
                            <ID>10</ID>
                            <AttribVariationID>9</AttribVariation>
                        </ModelAttribVarData>
                    </ModelAttribVar>   
                </AttribVariationData>
            </AttribVariation>              
        </AttributeData>
    </Attribute>
</ModelAttrib>  

我想将其转换为

<FeatureProduct>
    <ID>1</ID>
    <FeatureOptProduct>
        <ID>3</ID>
    </FeatureOptProduct>
    <FeatureOptProduct>
        <ID>9</ID>
    </FeatureOptProduct>
</FeatureProduct>

基本上,FeatureProduct节点映射到Attribute和FeatureOptProduct映射到AttribVariation。我有这样的ModelAttrib的多个节点,我想将它映射到第二个xml格式。 到目前为止,我的代码看起来像这样

  <xsl:template match = "/">
    <PolicyProduct>
      <ProductCode>
        <xsl:value-of select = "//ProdModelData/Code"/>
      </ProductCode>
      <PlanName>
        <xsl:value-of select = "//ProdModelData/Description"/>
      </PlanName>
      <LineOfBusiness>Annuity</LineOfBusiness>
      <AnnuityProduct>
        <xsl:for-each select= "//ModelAttrib/Attribute/AttributeData">
              <FeatureProduct>
                <ID>
                  <xsl:value-of select="ID" />
                </ID>
                <Name>
                  <xsl:value-of select="Description"/>
                </Name>
              </FeatureProduct>
            </xsl:for-each>
      </AnnuityProduct>
    </PolicyProduct>
  </xsl:template>

但我仍然坚持如何以

的形式获取属性标签下的所有attrib变种
<FeatureProduct>
 <FeatureOptProduct>
   <ID></ID>
 </FeaturOptProduct>
 <FeatureOptProduct>
   <ID></ID>
 </FeatureOptProduct>
</FeatureOptProduct>

1 个答案:

答案 0 :(得分:1)

您需要在

中添加嵌套的<xsl:for-each>循环
<xsl:for-each select= "//ModelAttrib/Attribute/AttributeData">

环。内部循环遍历所有<AttribVariation>个节点。

<xsl:for-each select="//ModelAttrib/Attribute/AttributeData">
    <FeatureProduct>
        <ID><xsl:value-of select="ID" /></ID>
        <Name><xsl:value-of select="Description" /></Name>
        <xsl:for-each select="AttribVariation"> <!-- inner loop -->
            <FeatureOptProduct>
                <ID><xsl:value-of select="AttribVariationData/ID" /></ID>
            </FeatureOptProduct>
        </xsl:for-each>
    </FeatureProduct>
</xsl:for-each>

此循环将产生以下输出

<AnnuityProduct>
  <FeatureProduct>
     <ID>1</ID>
     <Name/>
     <FeatureOptProduct>
        <ID>2</ID>
     </FeatureOptProduct>
     <FeatureOptProduct>
        <ID>9</ID>
     </FeatureOptProduct>
  </FeatureProduct>
</AnnuityProduct>

但是,根据所需的输出,您需要39作为<ID>的{​​{1}}。不确定这是否是拼写错误,但这种组合确实存在于<FeatureOptProduct>的子节点的相同XPath上。您可能需要根据<AttribVariation>所需的值更改XPath。