Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT xsl:sequence. What is it good for..?

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.

like image 961
user1942476 Avatar asked Jan 16 '13 15:01

user1942476


People also ask

What is xsl sequence?

Used to construct arbitrary sequences. It may select any sequence of nodes and/or atomic values, and essentially adds these to the result sequence. Category: instruction. Content: sequence-constructor.

What is the main use of an XSLT file?

The Extensible Stylesheet Language Transformation (XSLT) standard specifies a language definition for XML data transformations. XSLT is used to transform XML documents into XHTML documents, or into other XML documents.

What is the use of xsl template match?

The <xsl:template> element is used to build templates. The match attribute is used to associate a template with an XML element. The match attribute can also be used to define a template for the entire XML document. The value of the match attribute is an XPath expression (i.e. match="/" defines the whole document).


2 Answers

<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

<xsl:variable name="n" select="/html"/> 

Then

<xsl:copy-of select="$n"/> 

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).

<xsl:sequence select="$n"/> 

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.

<xsl:template match="a">    <x>    <xsl:sequence select="$n"/>    </x> </xsl:template> 

is the same as

<xsl:template match="a">     <x>     <xsl:copy-of select="$n"/>     </x> </xsl:template> 

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.

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:f="data:,f">      <xsl:variable name="s">         <x>hello</x>     </xsl:variable>      <xsl:template name="main">         ::         :: <xsl:value-of select="$s/x is f:s($s/x)"/>         :: <xsl:value-of select="$s/x is f:c($s/x)"/>         ::         :: <xsl:value-of select="count(f:s($s/x)/..)"/>         :: <xsl:value-of select="count(f:c($s/x)/..)"/>         ::     </xsl:template>      <xsl:function name="f:s">         <xsl:param name="x"/>         <xsl:sequence select="$x"/>     </xsl:function>      <xsl:function name="f:c">         <xsl:param name="x"/>         <xsl:copy-of select="$x"/>     </xsl:function>  </xsl:stylesheet> 

Produces

$ saxon9 -it main seq.xsl <?xml version="1.0" encoding="UTF-8"?> :: :: true :: false :: :: 1 :: 0 :: 

Here the results of xsl:sequence and xsl:copy-of are radically different.

like image 148
David Carlisle Avatar answered Oct 08 '22 17:10

David Carlisle


The most common use case for xsl:sequence is to return a result from xsl:function.

<xsl:function name="f:get-customers">   <xsl:sequence select="$input-doc//customer"/> </xsl:function> 

But it can also be handy in other contexts, for example

<xsl:variable name="x" as="element()*">   <xsl:choose>     <xsl:when test="$something">        <xsl:sequence select="//customer"/>     </xsl:when>     <xsl:otherwise>        <xsl:sequence select="//supplier"/>     </xsl:otherwise>   </xsl:choose> </xsl:variable> 

The key thing here is that it returns references to the original nodes, it doesn't make new copies.

like image 37
Michael Kay Avatar answered Oct 08 '22 16:10

Michael Kay