[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?
From: "Bob DuCharme" <bob@xxxxxxxxx>
Date: Mon, 11 Jun 2001 13:40:31 -0400
xsl count all preceding nodes
> How can my XSL count all the preceding nodes?  For example, when my
template
> is processing the <LINE>Test 3</LINE>, I want to count all nodes before
it,
> which would be 2 <LINE>s, 2 <PAGE>s, 2 <SECTION>s, 1 <CHAPTER>, and 1
<DOC>.

The template below will count the LINE, PAGE, SECTION, CHAPTER, and DOC
elements that precede each LINE element by using the 'preceding' axis and
the count() function. Keep in mind that in your example the count of DOC
elements will always be 0, because the only DOC element is the one used as a
document element and therefore enclosing the whole document, and when
element A is inside of element B, element B does not precede element A, even
if element B's start-tag does precede element A's start-tag. For one element
to be in another's 'preceding' axis, the whole thing has to precede that
element, not just its start-tag.

Bob DuCharme            www.snee.com/bob             <bob@
snee.com>      see http://www.snee.com/bob/xsltquickly for
info on new book "XSLT Quickly" from Manning Publications.

<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->

<xsl:template match="LINE">
  LINE contents:
  <xsl:apply-templates/>
  preceding LINE elements:
  <xsl:value-of select="count(preceding::LINE)"/>
  preceding SECTION elements:
  <xsl:value-of select="count(preceding::SECTION)"/>
  preceding PAGE elements:
  <xsl:value-of select="count(preceding::PAGE)"/>
  preceding DOC elements:
  <xsl:value-of select="count(preceding::DOC)"/>
</xsl:template>



 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.