Re: [xsl] position() outside for-each

Subject: Re: [xsl] position() outside for-each
From: "Andrew Welch" <andrew.j.welch@xxxxxxxxx>
Date: Wed, 14 Nov 2007 10:20:28 +0000
On 13/11/2007, Scott Trenda <Scott.Trenda@xxxxxxxx> wrote:
> David,
> I'm guessing that his stylesheet had a generic <xsl:apply-templates/>,
> and he was getting 2, 4, 6, 8, etc... the position including the
> whitespace text nodes.
> Charles,
> Use xsl:number, it'll usually give you exactly what you're looking for.
> :)

It's worth adding to that to say that position() gives you the
position of the current node within the list of selected nodes to

So as Scott says, if you have <xsl:apply-templates/> then the select
nodes to process will most likely be:

whitespace node, element, whitespace node, element etc the positions of the elements in that list are 2, 4 and so on
(with the whitespace nodes being 1 and 3)

The thing to remember is that the number returned by position() is
controlled by the stylesheet writer, not by the input.  For example if
you selected the nodes to process using:


position() would return 1

The other thing I've seen done is:

<xsl:for-each select="/root/item">
  <xsl:apply-templates select="."/>

Here position() would always return 1 within the item matching
template, because instead of a single list of nodes to process (where
each <item> has a position within that list) there are lots of lists
(equal to the number of <item>s) with a single <item> in each.

Andrew Welch

Current Thread