[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Proper syntax for counting all prior nodes in XPat

Subject: 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)
xpat axes
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
order.

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):
-----------------------
	<CHAPTER>
		<SECTION>
			<PAGE>
				<LINE>Test 4</LINE>
				<LINE>Test 5</LINE>
			</PAGE>
		</SECTION>
		<SECTION>
			<PAGE>
				<LINE>Test 6</LINE>
			</PAGE>
		</SECTION>
	</CHAPTER>


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

Then 

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

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.

Cheers,
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.