Re: [xsl] Using sum to Produce a total from combined documents

Subject: Re: [xsl] Using sum to Produce a total from combined documents
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Thu, 4 Jul 2002 12:02:43 +0100
Hi Eric,

> I need to get a total using a calculation.

Then you need a recursive template that steps through the nodes for
which you want to calculate a subtotal, keeping track of the total as
it goes:

<xsl:template name="total">
  <xsl:param name="quotes" select="equity-quote" />
  <xsl:param name="total" select="0" />
  <xsl:choose>
    <xsl:when test="$quotes">
      <xsl:variable name="quote" select="$quotes[1]" />
      <xsl:variable name="subtotal"
        select="$config/stocks/stock[@symbol=$quote/@symbol]/@shares *
                $quote/last-sale-price" />
      <xsl:call-template name="total">
        <xsl:with-param name="quotes"
                        select="$quotes[position() > 1]" />
        <xsl:with-param name="total"
                        select="$total + $subtotal" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$total" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

To insert the total, just call the template:

  <xsl:call-template name="total" />

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread