This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Using sum to Produce a total from combined documents


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]