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

Re: how to apply sum to a comma delimited list

Subject: Re: how to apply sum to a comma delimited list
From: Mike Brown <mike@xxxxxxxx>
Date: Wed, 15 May 2002 12:05:16 -0600 (MDT)
xslt comma delimited
Florin Dragu wrote:
> is there any way that I can use sum() functions in XSLT on a comma separated
> list (received as an output from a template)?

It would be easiest if when you built the comma-separated list, you created a
result tree fragment in which each value you want in the total is in a
separate element. The string-value of the fragment can still give you the list
output that you want, but you can also use XPath/XSLT's sum function on the
node-set equivalent of the fragment. xalan:nodeSet() (or exsl:node-set() if
you're using a more recent snapshot of Xalan) will convert the RTF to a
node-set.

The most portable alternative is to use a recursive template to dissect the
string.

Given XML:

<?xml version="1.0"?>
<some>
  <data>1</data>
  <data>2</data>
  <data>3</data>
  <data>5</data>
  <data>7</data>
  <data>11</data>
</some>

Here's a stylesheet that demonstrates both solutions:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:exsl="http://exslt.org/common"
                xmlns:xalan="http://xml.apache.org/xalan"
                exclude-result-prefixes="xalan exsl">
      
<xsl:output method="text"/>

<xsl:template match="/">
  <xsl:variable name="list">
    <xsl:for-each select="some/data">
      <dummyElement>
        <xsl:value-of select="."/>
      </dummyElement>
      <xsl:if test="position() != last()">
        <comma>,</comma>
      </xsl:if>
    </xsl:for-each>
  </xsl:variable>
  <xsl:text>The list is: </xsl:text>
  <xsl:value-of select="$list"/>
  <xsl:text>&#10;The sum is: </xsl:text>
  <xsl:choose>
    <xsl:when test="function-available('exsl:node-set')">
      <xsl:value-of select="count(exsl:node-set($list)/dummyElement)"/>
    </xsl:when>
    <xsl:when test="function-available('xalan:nodeSet')">
      <xsl:value-of select="sum(xalan:nodeSet($list)/dummyElement)"/> 
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="sum">
        <xsl:with-param name="str" select="string($list)"/>
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="sum">
  <xsl:param name="str"/>
  <xsl:param name="total" select="0"/>
  <xsl:choose>
    <xsl:when test="contains($str,',')">
      <xsl:call-template name="sum">
        <xsl:with-param name="str" select="substring-after($str,',')"/>
        <xsl:with-param name="total" select="$total + substring-before($str,',')"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$total + $str"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>

   - Mike
____________________________________________________________________________
  mike j. brown                   |  xml/xslt: http://skew.org/xml/
  denver/boulder, colorado, usa   |  resume: http://skew.org/~mike/resume/

 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.