Pravesh Kumar
Pravesh Kumar

Reputation: 37

What is the difference between 'xsl:copy-of' and 'xsl:sequence'?

What is the difference between 'xsl:copy-of' and 'xsl:sequence'

Upvotes: 3

Views: 1653

Answers (4)

Michael Kay
Michael Kay

Reputation: 163262

In very many situations they are interchangeable. One difference is that xsl:copy-of has more options (validation, copy-namespaces, etc). But most of the time you could use either.

There is a formal difference in that xsl:copy-of creates new nodes, while xsl:sequence returns a reference to existing nodes. That matters if, for example you use the "is" operator on the result, or if you use generate-id(), or if you navigate outside the subtree (e.g. to the parent of the copied node).

It's fair to say that 90% of the time people use xsl:copy-of in preference to xsl:sequence because that's what you did in XSLT 1.0, not because they really want a copy made.

Upvotes: 4

Alejandro
Alejandro

Reputation: 1882

Here you have a clear difference with an example.

This stylesheet:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:template match="/">
        <xsl:variable name="vCopy" as="document-node()">
            <xsl:copy-of select="."/>
        </xsl:variable>
        <xsl:variable name="vSequence" as="document-node()">
            <xsl:sequence select="."/>
        </xsl:variable>
        <xsl:sequence select="generate-id(.), 
                              generate-id($vCopy),
                              generate-id($vSequence)"/>
    </xsl:template>
</xsl:stylesheet>

It outputs with any input source something similar to:

d2047 d2048d2047 d2047

Do note: xsl:sequence retains node identity, xsl:copy creates a new node.

Upvotes: 3

Martin Honnen
Martin Honnen

Reputation: 167426

xsl:copy-of select="expression" evaluates expression and creates a deep copy of the sequence the expression evaluated to while xsl:sequence select="expression" evaluates expression and returns the sequence the expression evaluated to.

Depending on where you use the instruction that means that for instance a function using e.g. <xsl:sequence select="doc('file.xml')//foo"/> returns a sequence of foo elements from the file while <xsl:copy-of select="doc('file.xml')//foo"/> in the function returns a copy of the foo elements from the file.

As long as you are outputting to a result tree it doesn't usually matter whether you use one or the other, but if you select input nodes and want to preserve them and also don't want to waste memory it can often be more efficient to use xsl:sequence instead of xsl:copy-of.

Upvotes: 4

kjhughes
kjhughes

Reputation: 111491

xsl:copy-of copies the selected item as a deep copy.

xsl:sequence is used to construct an ordered list of nodes and/or atomic values.

Therefore, use xsl:copy-of to make a deep copy of a node that exists in the input document; use xsl:sequence to assemble an ordered list from possibly disparate nodes and primitive types.

Upvotes: 0

Related Questions