This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: determine node position


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()" />
     </xsl:if>
  </xsl:for-each>

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:

  <xsl:value-of
     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

---
Jeni Tennison
http://www.jenitennison.com/



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]