Re: [xsl] Accessing the Nth Occurrence of an Element

Subject: Re: [xsl] Accessing the Nth Occurrence of an Element
From: David Carlisle <davidc@xxxxxxxxx>
Date: Tue, 16 Sep 2008 12:25:42 +0100
> b) the -or-self part of the // axis is pointless when starting at the root

Actually I think I mislead you there.


two reasons, 

firstly //foo  always selects the same nodes as
decendant::foo even if used as a relative xpath. Because there is an
implied child axis in the final step.

//self::foo selects the same nodes as /descendant-or-self::foo


secondly you have to be careful about trees whose root node is an
element. If the current node is such a parentless foo element then
descendant::foo will not select the element, descendant-or-self::foo
will select the element and //foo is an error
 XPDY0050: The root of the tree containing the context item is not a document node





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

<xsl:variable name="x" as="element()">
 <foo a="1">
  <foo a="2"/>
  <foo a="3"/>
 </foo>
</xsl:variable>


<xsl:template name="main">
 <xsl:for-each select="$x">

<!--A:  <xsl:value-of select="//foo/@a"/>-->
B:  <xsl:value-of select="root()//foo/@a"/>
C:  <xsl:value-of select="root()//self::foo/@a"/>
<!--D:  <xsl:value-of select="/descendant::foo/@a"/>-->
E:  <xsl:value-of select="root()/descendant::foo/@a"/>
<!--F:  <xsl:value-of select="/descendant-or-self::foo/@a"/>-->
G:  <xsl:value-of select="root()/descendant-or-self::foo/@a"/>
 </xsl:for-each>
</xsl:template>

</xsl:stylesheet>



B:  2 3
C:  1 2 3

E:  2 3

G:  1 2 3

________________________________________________________________________
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