attribute_2-input.xml
<?xml version="1.0"?>
<products>
<product
id="p1">
<name>Delta</name>
<stock>4</stock>
</product>
</products>
attribute_2-stylesheet.xsl
<?xml version="1.0"?>
<xsl:stylesheet
version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output
indent="yes"/>
<xsl:template
match="/">
<Products>
<Product>
<xsl:attribute
name="id" select="products/product/@id"/>
<Name>
<xsl:value-of
select="products/product/name"/>
</Name>
<Stock>
<xsl:value-of
select="products/product/stock"/>
</Stock>
</Product>
</Products>
</xsl:template>
</xsl:stylesheet>
attribute_2-output.xml
<?xml version="1.0" encoding="UTF-8"?>
<Products>
<Product
id="p1">
<Name>Delta</Name>
<Stock>4</Stock>
</Product>
</Products>
The xsl:attribute element can be used for creating attributes even when the literal method is used for creating the element.
Updated 2009-03-19