This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: Column Calculations in XSLT
- To: xsl-list at mulberrytech dot com
- Subject: Re: Column Calculations in XSLT
- From: Warren Hedley <w dot hedley at auckland dot ac dot nz>
- Date: Thu, 29 Jun 2000 09:54:00 -0400
- CC: Walid dot Daccache at softsolutions dot fr
- Organization: Department of Engineering Science, The University of Auckland
- References: <395AEF5E.745A700E@softsolutions.fr>
- Reply-To: xsl-list at mulberrytech dot com
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 < 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