[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Summing multiple attributes

Subject: Re: Summing multiple attributes
From: Joerg Heinicke <joerg.heinicke@xxxxxx>
Date: Wed, 31 Jul 2002 20:25:00 +0200
mike muse
You don't need a recursive named template, but work recursively on the items:

<xsl:template match="items">
  <xsl:variable name="sum">
    <xsl:apply-templates select="item[1]"/>
  </xsl:variable>
  <xsl:value-of select="$sum"/>
</xsl:template>

<xsl:template match="item">
  <xsl:param name="sum" select="0"/>
  <xsl:apply-templates select="following-sibling::item[1]">
    <xsl:with-param name="sum" select="$sum + @count * @unitcost"/>
  </xsl:apply-templates>
</xsl:template>

Regards,

Joerg


Muse, Mike wrote:
Hi all,

I created a table based on the XML below, that has the following columns:
item, color, count, unitcost and sub-total (count*unitcost). The part I'm
having a problem with is calculating the total (i.e. sum of all sub-totals
in a table, for a member) for the table. I have put a subset of the XML
below as well as a sample of what the output should look like. The part I
can't get is the last line of the sample output.


I have been trying to use a recursive named template, but to no avail. I
have found several examples of summing one element or attribute using the
sum method. However, when I try to sum the product of count*unitcost, I
get an error because the result is not a nodeset.


Any help would be appreciated.

XML:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="demo.xsl"?>
</members>
	<member level="gold">
		<name>john smith</name>
		<phone type="home">555-5457</phone>
		<phone type="work">445-8524</phone>
		<phone type="cell">897-4684</phone>
		<street>124 pulse</street>
		<city>new york</city>
		<state>ny</state>
		<zip>05644</zip>
		<items>
			<item title="shirt" color="Purple" count="2"
unitcost="24.95"/>
			<item title="sport socks" color="white" count="2"
unitcost="12.95"/>
		</items>
	</member>
	. . .
</members>

Sample Output:
Item		Color	Count	UnitCost	SubTotal
shirt		Purple	2	24.95		49.90
sport socks	white	2	12.95		24.90
Total						74.80

Mike Muse
mike.muse@xxxxxxxxxx


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


Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.