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

Re: Accumulating Sum ( Sum Across Rows)

Subject: Re: Accumulating Sum ( Sum Across Rows)
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Wed, 3 Apr 2002 10:33:07 +0100
sum sum
Hi Kumar,

> Here is the XSL snippet i am using and adopting the sum() function
> you specified . It doesnt give me the sum rather it gives me the
> concatenation of numbers across the rows... For example in this XML
> example it gives 41 instead of 5 ( 4+1) can you throw some solution
> on this again to calculate SUM across ROWS ..I am attaching XML
> again if anyone wanna take a look at it ... Note : pete , I have
> changed XML slightly to look simple ...

The place where you're creating the sum is:

  <xsl:variable name="sumN">
    <xsl:for-each select="key('distinct-region', text())">
      <xsl:variable name="uid"   select="ancestor::vehicle/@id"/>
      <xsl:value-of
        select="sum(ancestor::data/review/vehicle[@idref=$uid])"/>
    </xsl:for-each>
  </xsl:variable>

In creating the variable sumN, you're iterating over each region_name
element and, for each of them, then find the id of their ancestor
vehicle, and then summing the string values of the vehicles with
matching idref (of which there's actually only one). So you get one
"sum" per region_name, concatenated together.

What you *want* is the sum of all the vehicles in the review section
that match the id of an ancestor vehicle of the region_name elements
that you're getting through the 'distinct-region' key.

To do this, I'd create a key that indexed the review/vehicle elements
by their idref attributes:

<xsl:key name="vehicles" match="review/vehicle" use="@idref" />

That means that you can get the vehicle with the idref 768 using:

  key('vehicles', '768')

What's more, if you pass a node set as the second argument of the key,
then you get all the vehicles whose idref is any of the values of the
nodes in the node set.

You can put together a node set containing all the ids that you're
interested in with the path:

  <xsl:variable name="vehicle-ids"
    select="key('distinct-region', text())/ancestor::vehicle/@id" />

So you can pass that to the 'vehicles' key to get the vehicles with
those idrefs:

  <xsl:variable name="vehicles"
                select="key('vehicles', $vehicle-ids)" />

and then you can sum their values together:

  <xsl:variable name="sumN" select="sum($vehicles)" />

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.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.