xsl:variable

  1. variable_1-input.xml

  2. <?xml version="1.0"?>
  3. <products>
  4.   <product id="p1">
  5.     <name>Delta</name>
  6.   </product>
  7. </products>
  1. variable_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:variable name="x" select="products/product"/>
  6.   <xsl:template match="/">
  7.     <Products>
  8.       <Product id="{$x/@id}">
  9.         <Name>
  10.           <xsl:value-of select="$x/name"/>
  11.         </Name>
  12.       </Product>
  13.     </Products>
  14.   </xsl:template>
  15. </xsl:stylesheet>
  1. variable_1-output.xml

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

Comment

  1. xsl:variable select="products"
  2. xsl:variable select="'products'"
  3. xsl:variable select="27"

The XSLT processor interprets "products" as an XPath expression, that is as the element "products". With an extra pair of apostrofes "'products'" become a string value, the word "products". Since an element name can not start with a digit, "27" is interpreted as the number "27".

Updated 2009-03-19