[xsl] Is there another way to do this?

Subject: [xsl] Is there another way to do this?
From: "Tim Lumley" <tlumley@xxxxxxxxxxxxxxx>
Date: Wed, 3 Mar 2004 21:19:04 +1100
Being new to XSL tree climbing (sequential tokenising HTML/XML/SGML for
years), I want to calculate the grand total for the saledetails.  I have
done my home work, around 3 XSLT books later I came up with this solution:

<XML/> (note: this is only a fragment of the xml):

<saledetails>
   <item>
      <sku>34860</sku>
      <description>Black felt-tip pens 0.4 mm</description>
      <units qpu="10">3</units>
      <unit.price gst="10">19.95</unit.price>
   </item>
   <item>
      <sku>49603</sku>
      <description>A4 Inkjet paper white</description>
      <units qpu="500">20</units>
      <unit.price gst="10">5.95</unit.price>
   </item>
</saledetails>

I use this call when I have matched="saledetails":

<xsl:call-template name="grandtotal">
   <xsl:with-param name="nodenumber" select="1"/>
   <xsl:with-param name="grandtotal" select="0"/>
</xsl:call-template>

To this template:

<xsl:template name="grandtotal">
   <xsl:param name="nodenumber"/>
   <xsl:param name="grandtotal"/>
   <xsl:choose>
      <xsl:when test="not(item[$nodenumber])">
         <xsl:value-of select="format-number($grandtotal, '#0.00')"/>
      </xsl:when>
      <xsl:otherwise>
         <xsl:call-template name="grandtotal">
            <xsl:with-param name="nodenumber" select="$nodenumber+1"/>
            <xsl:with-param name="grandtotal"
select="$grandtotal+(item[$nodenumber]/units *
item[$nodenumber]/unit.price)"/>
         </xsl:call-template>
      </xsl:otherwise>
   </xsl:choose>
</xsl:template>


This works but I'm just wondering is there a simpler method for doing this?


Cheers
Tim Lumley
tlumley@xxxxxxxxxxxxxxx



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


Current Thread