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

Re: Count() elements in range

Subject: Re: Count() elements in range
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Thu, 16 Oct 2003 15:43:10 -0400
xsl count preceding siblings
Chris,

This is nice. I was going to suggest a key-based grouping solution, but this works.

I particularly like the trick of passing the preceding siblings into a template as a parameter and then filtering from them using another parameter as a criterion: that technique could be useful....

Cheers,
Wendell

At 01:00 PM 10/16/2003, you wrote:
Figured it out...maybe it can help someone else!

<xsl:template match="/root/element">
  <xsl:choose>
    <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
="count(following-sibling::element[flag])"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise />
  </xsl:choose>
</xsl:template>

<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])"/>
</xsl:template>

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

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


======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================


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.