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
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