I have a XML File I want to transform to XML. And I need to dynamically set the name parameter of XML Tags, so it would be something like this:
<VALUE name="$varname"><xsl:value-of select="@value"/></VALUE>
I got something like this:
<xsl:for-each select="PRODTABLE/PRODTR">
<xsl:variable name="varname">
<xsl:copy-of select="PRODTD/PRAT/@name"/>
</xsl:variable>
<VALUE name="$varname">
<xsl:value-of select="PRODTD/PRAT/VALUE"/>
</VALUE>
</xsl:for-each>
But obviously, that doesn’t work. Is there a way to achieve this?
This is a FAQ.
The quick answer: An attribute value specified as
name="$varname"is literally the string “$varname”.The way in XSLT to produce an attribute with a dynamically computed value is either to use AVT (Attribute Value Template) or the
<xsl:attribute>instruction.Solution:
Use:
Your code may be re-written in this shorter way:
Instead of:
use: