[xsl] Re: Proper syntax for counting all prior nodes in XPath? (revised)

Subject: [xsl] Re: Proper syntax for counting all prior nodes in XPath? (revised)
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Tue, 12 Jun 2001 11:58:48 -0700 (PDT)
Jason Morris wrote:

> Originally I wanted all prior nodes.  Now I want to restrict my prior nodes
> to within a specific ancestor.
> I was originally using 
> <xsl:variable 
>   name="num_rows" 
> select="count(preceding::LINE)+count(preceding::PAGE)+count(preceding::SECTI
> ON)+count(preceding::CHAPTER)"/>
> as recommended by another list member.

Whoever recommended this was wrong!

What you mean by "prior" nodes includes all nodes before the current in document

Because the "preceding" and "ancestor" axes are non-overlapping, using only the
"preceding" axes in the above expression will fail to count the ancestors of the
current node.

A small example:

xml source (simplified):
				<LINE>Test 4</LINE>
				<LINE>Test 5</LINE>
				<LINE>Test 6</LINE>

Suppose your current node is <LINE>Test 6</LINE>.



is 1 and not 2, as you said you expected.

The correct expression counting ***all*** prior nodes must count all "ancestor"
nodes in addition to the "preceding" nodes:

count(//LINE[contains(.,'6')]/preceding::PAGE |
//LINE[contains(.,'6')]/ancestor::PAGE )

is 2

Hope this helped.

Dimitre Novatchev.

Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35 
a year!  http://personal.mail.yahoo.com/

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

Current Thread