Re: [xsl] determine node position

Subject: Re: [xsl] determine node position
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Tue, 10 Apr 2001 00:52:08 +0100
Hi Kristof,

> with the value of an attribute i'm trying to determine the node
> position number of the one element that contains that specific
> attribute value.

The position() of a node gives you the position of the node in the
current node list - the nodes that are currently being processed in
the order that they're being processed in. So one method is to make
the current node list be the list of nodes that you might be
interested in, and then test each of the nodes as they're processed to
see if they're the single node that you are interested in.  That
node's position() is the number that you're after.

In other words, do an xsl:for-each over each of the item elements.  If
the ID is the one that you want, then output the value of the
position() of that item element:

  <xsl:for-each select="item">
     <xsl:if test="@ID = 'thursday'">
        <xsl:value-of select="position()" />

That's not a particularly good way of doing it, though, because you
have to go over every single item element each time.  Better is to
find the node that you want, and then count how many preceding
siblings it has:

     select="count(item[@ID = 'thursday']/preceding-sibling::item)" />

Depending on how many elements you have, you may find it more
efficient to put attributes on the source to indicate the position of
each element. If there are lots of item elements, then collecting up
all the preceding siblings and counting them might take a bit of time.

I hope that helps,


Jeni Tennison

 XSL-List info and archive:

Current Thread