xsl:attribute (1)

  1. attribute_1-input.xml

  2. <?xml version="1.0"?>
  3. <products>
  4.   <product id="p1">
  5.     <name>Delta</name>
  6.     <stock>4</stock>
  7.   </product>
  8. </products>
  1. attribute_1-stylesheet.xsl

  2. <?xml version="1.0"?>
  3. <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  4.   <xsl:output indent="yes"/>
  5.   <xsl:template match="/">
  6.     <xsl:element name="Products">
  7.       <xsl:element name="Product">
  8.         <xsl:attribute name="id"><xsl:value-of select="products/product/@id"/></xsl:attribute>
  9.         <xsl:element name="Name">
  10.           <xsl:value-of select="products/product/name"/>
  11.         </xsl:element>
  12.         <xsl:element name="Stock">
  13.           <xsl:value-of select="products/product/stock"/>
  14.         </xsl:element>
  15.       </xsl:element>
  16.     </xsl:element>
  17.   </xsl:template>
  18. </xsl:stylesheet>
  1. attribute_1-output.xml

  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <Products>
  4.   <Product id="p1">
  5.     <Name>Delta</Name>
  6.     <Stock>4</Stock>
  7.   </Product>
  8. </Products>

Comment

In XSLT 2.0 we could also have said:

<xsl:attribute name="id" select="products/product/@id"/>

Updated 2009-03-19