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: Accumulating Sum ( Sum Across Rows)


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]