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: Summing multiple attributes


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@elpaso.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]