xsl:attribute (2)

  1. attribute_2-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_2-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.     <Products>
  7.       <Product>
  8.         <xsl:attribute name="id" select="products/product/@id"/>
  9.         <Name>
  10.           <xsl:value-of select="products/product/name"/>
  11.         </Name>
  12.         <Stock>
  13.           <xsl:value-of select="products/product/stock"/>
  14.         </Stock>
  15.       </Product>
  16.     </Products>
  17.   </xsl:template>
  18. </xsl:stylesheet>
  1. attribute_2-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

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