Re: [xsl] xsl:sequence

Subject: Re: [xsl] xsl:sequence
From: "andrew welch" <andrew.j.welch@xxxxxxxxx>
Date: Wed, 9 Aug 2006 12:16:31 +0100
On 8/9/06, tom tom <tomxsllist@xxxxxxxxxxx> wrote:
Can anyone tell me when I should be using value-of, when i should be using
copy-of and when I should be using sequence?

The difference between value-of and copy-of is pretty straightforward, the string value of the element vs a deep-copy of the element. The difference between copy-of and xsl:sequence is more subtle and can be highlighted with this example:

XML Source:
<foo>a<bar>b</bar>c</foo>

XSLT:
<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:variable name="sequ" as="element()">
	<xsl:sequence select="/foo/bar"/>
</xsl:variable>

<xsl:variable name="copy" as="element()">
	<xsl:copy-of select="/foo/bar"/>
</xsl:variable>

<xsl:template match="/">
	<result>
		<sequence><xsl:copy-of select="$sequ/parent::*"/></sequence>
		<copy><xsl:copy-of select="$copy/parent::*"/></copy>
	</result>	
</xsl:template>

</xsl:stylesheet>

Output:

<result>
	<sequence>
		<foo>a
			<bar>b</bar>c
		</foo>
	</sequence>
	<copy/>
</result>

Here you can see that the sequence has copied the parent node through,
while the copy hasn't.

This is because the variable holding the sequence contains a pointer
to the element in the orinal source, and therefore knows its parent.
The variable holding the copy of the element hold just a copy, there
is no parent so nothing is copied to the output.

I hope this helps explain it a little better - sequence refers to
elements in there orginal source location, copy-of creates a
deep-equal copy.

cheers
andrew

Current Thread