I know the following question is a little bit of beginners but I need your help to understand a basic concept.
I would like to say first that I'm a XSLT programmer for 3 years and yet there are some new and quite basics things I've been learning here I never knew (In my job anyone learns how to program alone, there is no course involved).
My question is:
What is the usage of xsl:sequence
?
I have been using xsl:copy-of
in order to copy node as is, xsl:apply-templates
in order to modifiy nodes I selected and value-of
for simple text.
I never had the necessity using xsl:sequence
. I would appreciate if someone can show me an example of xsl:sequence
usage which is preferred or cannot be achieved without the ones I noted above.
One more thing, I have read about the xsl:sequence
definition of course, but I couldn't infer how it is useful.
Well to return a value of a certain type you use
xsl:sequence
asxsl:value-of
despite its name always creates a text node (since XSLT 1.0). So in a function body you useto return an
xs:integer
value, you would useto return an
xs:string
value andto return an
xs:date
value and so on. Of course you can also return sequences with e.g.<xsl:sequence select="1, 2, 3"/>
.You wouldn't want to create a text node or even an element node in these cases in my view as it is inefficient.
So that is my take, with the new schema based type system of XSLT and XPath 2.0 a way is needed to return or pass around values of these types and a new construct was needed.
[edit]Michael Kay says in his "XSLT 2.0 and XPath 2.0 programmer's reference" about
xsl:sequence
: "This innocent looking instruction introduced in XSLT 2.0 has far reaching effects on the capability of the XSLT language, because it means that XSLT instructions and sequence constructors (and hence functions and templates) become capable of returning any value allowed by the XPath data model. Without it, XSLT instructions could only be used to create new nodes in a result tree, but with it, they can also return atomic values and references to existing nodes.".Another use is to create a tag only if it has a child. An example is required :
Somewhere in your XSLT :
You may see the demo here : http://xsltransform.net/eiZQaFz
It is way better than testing each tag like this :
Because you would end up editing it in two places. Also the processing speed would depend on which tags are in your imput. If it is the last one from your test, the engine will test the presence of everyone before. As $foo/node() is an idioms for "is there a child element ?", the engine can optimize it. Doing so, you ease the life of everyone.
The most common use case for xsl:sequence is to return a result from xsl:function.
But it can also be handy in other contexts, for example
The key thing here is that it returns references to the original nodes, it doesn't make new copies.
<xsl:sequence>
on an atomic value (or sequence of atomic values) is the same as<xsl:copy-of>
both just return a copy of their input. The difference comes when you consider nodes.If $n is a single element node, eg as defined by something like
Then
Returns a copy of the node, it has the same name and child structure but it is a new node with a new identity (and no parent).
Returns the node $n, The node returned has the same parent as $n and is equal to it by the
is
Xpath operator.The difference is almost entirely masked in traditional (XSLT 1 style) template usage as you never get access to the result of either operation the result of the constructor is implicitly copied to the output tree so the fact that
xsl:sequence
doesn't make a copy is masked.is the same as
Both make a new element node and copy the result of the content as children of the new node
x
.However the difference is quickly seen if you use functions.
Produces
Here the results of
xsl:sequence
andxsl:copy-of
are radically different.