Subject: [xsl] Re: Summing multiple attributes From: Dimitre Novatchev <dnovatchev@xxxxxxxxx> Date: Wed, 31 Jul 2002 12:35:49 -0700 (PDT) |
"Muse, Mike" <Mike dot Muse at elpaso dot com> 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 Hi Mike, Goerg already provided one solution to your problem. It is not very efficient, because each multiplication "count*unitcost" is performed for a second time (you performed it once to calculate the subtotal). Also, two passes on the data are performed. Here's another solution, which calculates both the subtotals and the total in just one pass, and performs every multiplication just once. I'm using the "scanl" template from FXSL: testScanl2.xsl: -------------- <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:vendor="urn:schemas-microsoft-com:xslt" xmlns:myTotal="f:myTotal" xmlns:myParam="f:myParam" exclude-result-prefixes="xsl vendor myParam myTotal" > <xsl:import href="scanl.xsl"/> <!-- to be applied on numList.xml --> <xsl:output omit-xml-declaration="yes" indent="yes"/> <myTotal:myTotal/> <myParam:myParam> <subtotal>0</subtotal> <total>0</total> </myParam:myParam> <xsl:template match="/"> <xsl:variable name="vFun" select="document('')/*/myTotal:*[1]"/> <xsl:variable name="vZeros" select="document('')/*/myParam:*[1]"/> <xsl:variable name="vrtf-calcResults"> <xsl:call-template name="scanl"> <xsl:with-param name="pFun" select="$vFun"/> <xsl:with-param name="pQ0" select="$vZeros" /> <xsl:with-param name="pList" select="/*/*"/> </xsl:call-template> </xsl:variable> <xsl:variable name="vcalcResults" select="vendor:node-set($vrtf-calcResults)/*[position() > 1]"/> <xsl:for-each select="$vcalcResults"> <xsl:value-of select="concat(subtotal, '
')"/> </xsl:for-each> <xsl:value-of select="$vcalcResults[last()]/total"/> </xsl:template> <xsl:template match="myTotal:*"> <xsl:param name="pArg1" select="/.."/> <xsl:param name="pArg2" select="/.."/> <xsl:variable name="vsubTotal" select="$pArg2/@count * $pArg2/@unitcost"/> <subtotal> <xsl:value-of select="$vsubTotal"/> </subtotal> <total> <xsl:value-of select="$pArg1/total + $vsubTotal"/> </total> </xsl:template> </xsl:stylesheet> When the above transformation is applied on the following xml document (just a node from your original xml document): testScanl2.xml: -------------- <items> <item title="shirt" color="Purple" count="2" unitcost="24.95"/> <item title="sport socks" color="white" count="2" unitcost="12.95"/> </items> The result is: 49.9 25.9 75.8 Hope this helped. ===== Cheers, Dimitre Novatchev. http://fxsl.sourceforge.net/ -- the home of FXSL __________________________________________________ Do You Yahoo!? Yahoo! Health - Feel better, live better http://health.yahoo.com XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] Expression Logic Problem, Charles Knell | Thread | |
Re: [xsl] Expression Logic Problem, Joerg Heinicke | Date | RE: [xsl] Expression Logic Problem, Renick, Garrel |
Month |