RE: [xsl] Is there another way to do this?

Subject: RE: [xsl] Is there another way to do this?
From: "Michael Kay" <mhk@xxxxxxxxx>
Date: Wed, 3 Mar 2004 13:16:54 -0000
Other possible ways to tackle this are

(a) by creating a temporary tree (result tree fragment) containing the
values to be summed, and then using sum(). This requires the node-set()
extension function

(b) implement a higher-order function using Dimitre Novatchev's FXSL library

(c) use an XPath 2.0 "for" expression:

sum(for $i in //item return $i/units * $i/unit.price)

Michael Kay

# -----Original Message-----
# From: owner-xsl-list@xxxxxxxxxxxxxxxxxxxxxx [mailto:owner-xsl-
# list@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of Tim Lumley
# Sent: 03 March 2004 10:19
# To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
# Subject: [xsl] Is there another way to do this?
# 
# 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



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


Current Thread