RE: [xsl] counting nodes before a certain node

Subject: RE: [xsl] counting nodes before a certain node
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Mon, 30 Nov 2009 19:01:32 -0500

At 06:02 PM 11/30/2009, Mike wrote:
Given XSLT 2.0, you can select two elements $a and $b, and then the number
of elements between these two is

count($b/preceding-sibling::*) - count($b/preceding-sibling::*) - 1

This is probably more efficient than

count(*[. >> $a and $b >> .])

but you can use that if it's more convenient.

This answers the question as posed.

A better solution, however, might be available if you specified your count differently.

For example, you might be better off asking "how do I count notes elements that do not have book or author element ancestors", or "how do I count notes elements that are directly inside books, but not deeper". (From your problem statement it's impossible to know whether one of these might not be the case.)


> -----Original Message-----
> From: a kusa [mailto:akusa8@xxxxxxxxx]
> Sent: 30 November 2009 22:40
> To: xsl-list
> Subject: [xsl] counting nodes before a certain node
> Hi
> How do I count nodes up to a certain point in XSLT?
> Example:
> My xml file looks like this:
> <books>
> <book>
> <title>First title</book>
> </book>
> <notes>The following books talk about history.</notes>
> <notes>For further information please log on to </notes>
> <notes>Thank you for choosing.. </notes> <book>
> <title>Abraham Lincoln </title> </book>
> <authors>
> sample data
> </authors>
> <notes>Please call... </notes>
> <notes>Copyright Information </notes>
> <book>
> sample data
> </book>
> </books>
> In my XSLT file, I want to count all <notes> just before the
> second <book> element but immediately after the first book
> element or just after <authors>.
> How do I count all the notes elements here?

Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.      
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
  Mulberry Technologies: A Consultancy Specializing in SGML and XML

Current Thread