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

Re: Column Calculations in XSLT

Subject: Re: Column Calculations in XSLT
From: Warren Hedley <w.hedley@xxxxxxxxxxxxxx>
Date: Thu, 29 Jun 2000 09:54:00 -0400
xslt sum column
SoftLiban DACCACHE Walid wrote:
> 
> Hi to all,
> 
> I was doing straight summation on the column level using the
> sum(Range/purchase) function and was doing great. But when I tried more
> complex calculations like
> 
> sum(Range/purchase * Range/Quantity) I couldn't generate the output
> file.

I think the problem is that sum() takes a nodeset, calls
number(string(.)) on each node, and sums the result.

The "*" operator requires two numbers, and can't be used to
combine a set of nodes - you will need to do that iteration
yourself.

You need a recursive template like the one below - not tested!

<xsl:template match="Range">
  <xsl:variable name="sum">
    <xsl:call-template name="sum_purchase_times_quantity" />
  </xsl:variable>
</xsl:template>

<xsl:template name="sum_purchase_times_quantity">
<!-- private parameters -->
  <xsl:param name="position" select="'1'" />
  <xsl:param name="sum"      select="'0'" />

  <xsl:variable name="last_purchase"
      select="count(purchase)" />

  <xsl:choose>
    <xsl:when test="$position &lt; count(purchase)">
      <xsl:call-template name="sum_purchase_times_quantity">
        <xsl:with-param name="position" select="$position+1" />
        <xsl:with-param name="sum" select="$sum +
          ( number(purchase[$position]/text()) *
            number(quantity[$position]/text()) )" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$sum" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

-- 
Warren Hedley


 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.