RE: [xsl] index of an element among its type

Subject: RE: [xsl] index of an element among its type
From: "Andrew Welch" <ajwelch@xxxxxxxxxxxxxxx>
Date: Thu, 10 Feb 2005 13:45:16 -0000
> I want to find the position() of an element in an xml file
> among its type. for example <book> <title>Introduction to
> Algorithms</title> <author>Cormen</author>
> <author>Leiserson</author> ... <author>Rivest</author> </book>
> I want to get (1) for Cormen, (2) for Leiserson and (3) for
> Rivest. using the normal position() function will return 2,3
> and 5 resepctively.

The position() function returns the index of the element within the
current element list.  Between each <author> element you have a
whitespace text node, which is being included in the current element
list giving you the results you're seeing.

There are many ways to solve this, but without seeing your stylesheets
I'll just have to list a few:

- Use count(preceding-sibling::author) instead of position()
- Be more specific when selecting processing the <author> elements
- If you are using node(), use * instead as this only selects element
- Use xsl:strip-space to remove unwanted presentational whitespace

If you post the template it would be easier to suggest a fix...


Current Thread