This example shows the use of <xsl:attribute name="{name()}"/>.
Note To test this example, you need to use a script. For more information, see Initiate XSLT in a Script.
XML File (attribute.xml)
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="attribute.xsl"?> <root> <bar>bar1</bar> <bar>bar2</bar> <bar>bar3</bar> <bar>bar4</bar> <bar>bar5</bar> </root>
XSLT File (attribute.xsl)
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" indent="yes"/>
<xsl:template match="//bar">
<xsl:element name="myElement">
<xsl:attribute name="{name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Output
There is no formatted output. This is the processor output:
<?xml version="1.0"?> <myElement bar="bar1" /> <myElement bar="bar2" /> ... <myElement bar="bar5" />