Re: [xsl] Count() elements in range

Subject: Re: [xsl] Count() elements in range
From: Chris.Pearson@xxxxxxxxxxxx
Date: Thu, 16 Oct 2003 13:00:28 -0400
Figured it out...maybe it can help someone else!

<xsl:template match="/root/element">
    <xsl:when test="(following-sibling::element[1][child::flag]) or
(count(following-sibling::element) = 0)"> <!-- only call when it's the last
element in a series-->
      <xsl:call-template name="countFlags">
        <xsl:with-param name="nodeList" select="preceding-sibling::element"
        <xsl:with-param name="followingFlags" select
    <xsl:otherwise />

<xsl:template name="countFlags">
  <xsl:param name="nodeList"/>
  <xsl:param name="followingFlags"/>
  <xsl:value-of select="1 + count
($nodeList[count(following-sibling::element[flag]) = $followingFlags])"/>

gives output:
Element A1: 1
Element B4: 4
Element C2: 2
Element D3: 3

<element number="A1"><!--start a series of 1 -->
</element><!-- only 1 in series, so count is only 1 -->
<element number="B1"><!-- start a series of 4 -->
<element number="B2"/>
<element number="B3"/>
<element number="B4"/><!-- count the previous 3 elements -->
<element  number="C1"><!-- start a series of 2 -->
<element  number="C2"/><!--count the previous 1 element -->
<element  number="D1"><!-- start a series of 3 -->
<element  number="D2" />
<element  number="D3" /><!-- count the previous 2 elements -->

Chris Pearson
Agilisys Automotive

 XSL-List info and archive:

Current Thread