[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: sum of the evaluated values
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, ":")')" /> 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
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|