sequence_1-input.xml
<?xml version="1.0"?>
<products>
<product
id="p1">
<name>Delta</name>
<price>800</price>
<stock>4</stock>
<country>Denmark</country>
</product>
</products>
sequence_1-stylesheet.xsl
<?xml version="1.0"?>
<xsl:stylesheet
version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output
indent="yes"/>
<xsl:template
match="/">
<xsl:variable
name="x" as="element()">
<xsl:sequence
select="products/product/price"/>
</xsl:variable>
<xsl:variable
name="y" as="element()">
<xsl:copy-of
select="products/product/price"/>
</xsl:variable>
<tests>
<test>
<xsl:value-of
select="$x/../country"/>
</test>
<test>
<xsl:value-of
select="$y/../country"/>
</test>
</tests>
</xsl:template>
</xsl:stylesheet>
sequence_1-output.xml
<?xml version="1.0" encoding="UTF-8"?>
<tests
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<test>Denmark</test>
<test/>
</tests>
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