xsl:sequence

  1. sequence_1-input.xml

  2. <?xml version="1.0"?>
  3. <products>
  4.   <product id="p1">
  5.     <name>Delta</name>
  6.     <price>800</price>
  7.     <stock>4</stock>  
  8.     <country>Denmark</country>
  9.   </product>
  10. </products>
  1. sequence_1-stylesheet.xsl

  2. <?xml version="1.0"?>
  3. <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  4.   <xsl:output indent="yes"/>
  5.   <xsl:template match="/">
  6.     <xsl:variable name="x" as="element()">
  7.       <xsl:sequence select="products/product/price"/>
  8.     </xsl:variable>
  9.     <xsl:variable name="y" as="element()">
  10.       <xsl:copy-of select="products/product/price"/>
  11.     </xsl:variable>
  12.     <tests>
  13.       <test>
  14.         <xsl:value-of select="$x/../country"/>
  15.       </test>
  16.       <test>
  17.         <xsl:value-of select="$y/../country"/>
  18.       </test>
  19.     </tests>
  20.   </xsl:template>
  21. </xsl:stylesheet>
  1. sequence_1-output.xml

  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <tests xmlns:xs="http://www.w3.org/2001/XMLSchema">
  4.   <test>Denmark</test>
  5.   <test/>
  6. </tests>

Comment

The elements xsl:sequence and xsl:copy-of can in most situations be substituted for one another. But in our example there is a big difference. Both variables contain the price element and its value. But the price element in the variable made with xsl:sequence is only a reference. With XPath expressions we can still move to parent element and into another child. With xsl:copy-of only what is copied is in the variable.

Updated 2009-03-19