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: Proper syntax for counting all prior nodes in XPath? (revised)


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


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