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

Subject: Re: [xsl] Proper syntax for counting all prior nodes in XPath?
From: "Bob DuCharme" <bob@xxxxxxxxx>
Date: Mon, 11 Jun 2001 13:40:31 -0400
> 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