[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: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Wed, 31 Jul 2002 12:35:49 -0700 (PDT)
mike g rg
"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, '&#xA;')"/>
    </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

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.