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

Re: sum of the evaluated values

Subject: Re: sum of the evaluated values
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Thu, 22 Nov 2001 10:33:09 +0000
xsl sum
Hi Al,

> i have xml:
>   <event type="1" time="3:00">
>   <event type="0" time="2:00">
>   <event type="1" time="4:00">
>   <event type="1" time="5:00">
> and i need sum of the minutes: number(substring-before(@time,':'))
> only where @type=1.
>
> How it can  be done?

Several ways. If you're using Saxon, you could use its extension
saxon:sum() function:

  <xsl:value-of
    select="saxon:sum(event[@type = 1],
              saxon:expression('substring-before(@time,
                                                 &quot;:&quot;)')" />

Otherwise, if you're happy using extension functions then you could
build a result tree fragment of elements with the relevant values,
convert it to a node set using the appropriate node-set() extension
function for your processor, and then use the sum() function:

  <xsl:variable name="hours">
    <xsl:for-each select="event[@type = 1]">
      <hour>
        <xsl:value-of select="substring-before(@time, ':')" />
      </hour>
    </xsl:for-each>
  </xsl:variable>
  <xsl:value-of select="sum(exsl:node-set($hours)/hour)" />

If you want a pure XSLT 1.0 method, one way is to create your own
recursive template especially for the events that you're using, as
follows:

<xsl:template name="totalTime">
  <xsl:param name="events" select="event" />
  <xsl:param name="subtotal" select="0" />
  <xsl:choose>
    <xsl:when test="$events">
      <xsl:call-template name="totalTime">
        <xsl:with-param name="events"
                        select="$events[position() > 1]" />
        <xsl:with-param name="subtotal"
          select="$subtotal + substring-before($events[1], ':')" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$subtotal" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

You can call the above template simply with:

  <xsl:call-template name="totalTime" />

from the parent of the event elements.

Finally, you could use a more generic template - I'm sure that Dimitre
has one for summing - in which case all you have to do is write the
code for the template that gives the value for the event. I'm sure
Dimitre will tell you more about how to use that if you ask.

I hope that helps,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 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.