Subject: Re: [xsl] Trying to understand root-less or document-node-less nodes From: David Carlisle <davidc@xxxxxxxxx> Date: Mon, 17 Sep 2007 13:43:12 +0100 |
the reference to xsl:function just confuses the issue in a way. as you can see the same behaviour just with xsl:variable with and without an as attribute. Remember that in XPath2 a sequence may hold more than just nodes and (in XPath 1 and 2) axis selectors such as following-sibling:: always work on the original document not the current sequence (current node list in xslt2) so consider a document node $x with structure <x> <a/> <b/> <b/> <c/> </x> then if you go <xsl:variable name="y" select="$x/x/b"/> then $y is a sequence of the two (original) b nodes. following-sibling:: works but works relative to the original nodes %y/following-sibling::* selects the second b node and the c node. Contrast that with <xsl:variable name="y2"> <xsl:copy-of select=$x/x/b"/> </xsl:variable> Now $y2 is a sequence of 1 document node with children _copies_ of the two b nodes. so $y2/following-sibling::* is empty as the/following-sibling::* h document node has no siblings and $y2/*/following-sibling::* is a single b node, being the copy of te hb copied into the variable. In XSLT1 the only way to generate a sequence (node set) of nodes in teh original document is to use the select= for,, but in xslt2 the same can be achieved using as= <xsl:variable name="y3" as="element()"> <xsl:sequence select=$x/x/b"/> </xsl:variable> with an as attribute, the sequence of items constructed is returned directly as the value of the variable, no implicit document node is created. so $y3 is exactly the same as $y, but of course this new form is more versatile as you are not restricted to a single xpath statement <xsl:variable name="y3" as="element()"> <xsl:sequence select=$x/x/b"/> <foo/> <xsl:sequence select=$x/x/c"/> </xsl:variable> is a sequence of 5 elements two bs, a foo and a c, but note that teh b's and c's are siblinngs of each other (and all are siblings of $x/x/a) but that <foo/> is a new parentless element not a sibling of anything. xsl:function (and xsl:template, xsl:if and several other instructions) work like xsl:variable with as="item()". That is, their content generates a sequence which is returned as the value of the instruction. xsl:variable (and xsl:param) without an as attribute generate a sequence in the same way, but then as an additional step generate an implict document node and _copy_ the generated sequence as children of that node (which implies generating text nodes corresponding to any atomic values in the sequence) David ________________________________________________________________________ The Numerical Algorithms Group Ltd is a company registered in England and Wales with company number 1249803. The registered office is: Wilkinson House, Jordan Hill Road, Oxford OX2 8DR, United Kingdom. This e-mail has been scanned for all viruses by Star. The service is powered by MessageLabs. ________________________________________________________________________
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Trying to understand root, Abel Braaksma | Thread | Re: [xsl] Trying to understand root, Abel Braaksma |
Re: [xsl] Client-side compiled XSLT, Robert Koberg | Date | Re: [xsl] Trying to understand root, Andrew Welch |
Month |