Re: Performing an expression on attributes of a node-set

Subject: Re: Performing an expression on attributes of a node-set
From: Nick Browne <NickBrowne@xxxxxxxxxxxxxxx>
Date: Wed, 27 Sep 2000 20:33:14 +0100
Jim Sproull wrote:

>  But I need the sum of each (price*qty).  I tried everything I could think
> of, but no luck.  Any clues on how to do this?

A solution follows. However, as an alternative, assuming you are in control of
the source document, could you include another attribute which is simply @qty *
@price. You could then use sum( .. ). Sometimes a bit of redundancy is not a
bad thing !

Otherwise :

   <xsl:variable name="Items" select="//Item"/>

   <xsl:variable name="TotalValue">
    <xsl:call-template name="Total">
     <xsl:with-param name="Items" select="$Items"/>
     <xsl:with-param name="RunningTotal"    select="0"/>
    </xsl:call-template>
  </xsl:variable>

      Total   = <xsl:value-of select="format-number($TotalValue, '####0.0#')"/>

 </xsl:template>

 <xsl:template name="Total">
  <xsl:param name="Items"/>
  <xsl:param name="RunningTotal"/>
  <xsl:choose>
   <xsl:when test="not($Items)">
    <xsl:copy-of select="$RunningTotal"/>
   </xsl:when>
   <xsl:otherwise>
  <xsl:variable name="CurrentTotal"
         select="$RunningTotal + ($Items[1]/@qty * $Items[1]/@price)"/>

   <xsl:call-template name="Total">
    <xsl:with-param name="Items" select="$Items[position()>1]"/>
    <xsl:with-param name="RunningTotal" select="$CurrentTotal"/>
   </xsl:call-template>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

Regards

Nick Browne
Slipstone Ltd



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


Current Thread