Passing node name created runtime to xsl:value-of select



My Input XML :



<Book>
<Chapter>
<Pages value="20" />
<Note1Code value="1" />
<Note1Description value="This is just for reference" />
<Note1Level value="Avg" />

<Note2Code value="2" />
<Note2Description value="This is high important note" />
<Note2Level value="Imp" />
</Chapter>
</Book>


I want to transform into :



<Book>
<Chapter>
<Pages value="20" />

<Note>
<Code>1</Code>
<Description>This is just for reference</Description>
<Level>Avg</Level>
</Note

<Note>
<Code>2</Code>
<Description>This is high important note</Description>
<Level>Imp</Level>
</Note
</Chapter>
</Book>


Here is my XSLT :



<xsl:stylesheet version="1.0" xmlns:xsl="http://ift.tt/tCZ8VR">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[substring(name(), 1, 4) = 'Note' and substring(name(),6) = 'Code']">

<xsl:variable name="pdIdx" select="substring(name(), 5, 1)"/>
<xsl:variable name="pdNode" select="concat('Note',$pdIdx,'Description/@value')"/>
<xsl:variable name="pd" select="$pdNode"/>
<Code>
<xsl:value-of select="@value"/>
</Code>
<Description>
<xsl:value-of select="$pdNode"/>
</Description>
</xsl:template>
</xsl:stylesheet>


I am trying to create the node name dynamically and then trying to get the value in select attribute. However, it is not working. The output is : The description tag is taking the node name and not its value



<Book>
<Chapter>
<Pages value="20"/>

<Code>1</Code>
<Description>Note1Description/@value</Description>
<Node1Description value="This is just for reference"/>
<Note1Level value="Avg"/>

<Code>2</Code>
<Description>Note2Description/@value</Description>
<Node2Description value="This is high important note"/>
<Note2Level value="Imp"/>
</Chapter>
</Book>

No comments:

Post a Comment