Hitzz91
Hitzz91

Reputation: 39

How to get child nodes inside a parent node using xslt

I have an xml in format of

<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>  

And I want to convert it into

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

Basically the FeatureProduct Node maps to Attribute and FeatureOptProduct maps to AttribVariation. And I have multiple nodes of ModelAttrib like this and I want to map it to the second xml format. So far my code looks something like this

  <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>

But I am stuck on how to get all the attribvariation under the attribute tag in the form of

<FeatureProduct>
 <FeatureOptProduct>
   <ID></ID>
 </FeaturOptProduct>
 <FeatureOptProduct>
   <ID></ID>
 </FeatureOptProduct>
</FeatureOptProduct>

Upvotes: 1

Views: 625

Answers (1)

Aniket V
Aniket V

Reputation: 3247

You need to add a nested <xsl:for-each> loop within the

<xsl:for-each select= "//ModelAttrib/Attribute/AttributeData">

loop. The inner loop with run over all the <AttribVariation> nodes.

<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>

This loop will product the following output

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

However as per the desired output, you need 3 and 9 as the <ID> of <FeatureOptProduct>. Not sure whether this is a typo but this combination does on lie on the same XPath of the child nodes of <AttribVariation>. You may need to change the XPath based on the required value of FeatureOptProduct/ID.

Upvotes: 1

Related Questions