Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Summing a list

Reply
Thread Tools

Summing a list

 
 
shockride@gmail.com
Guest
Posts: n/a
 
      09-22-2005
I know this should be easy, but I have been banging my head against it
for too long...

Here is my XML (names changed blah blah)

<Factory>
<Containers>
<Container>
<Country>USA</Country>
<Fruit><Type>Apple</Type><Amount>10</Amount></Fruit>
</Container>
<Container>
<Country>USA</Country>
<Fruit><Type>Banana</Type><Amount>12</Amount></Fruit>
</Container>
<Container>
<Country>USA</Country>
<Fruit><Type>Apple</Type><Amount>13</Amount></Fruit>
</Container>
<Container>
<Country>USA</Country>
<Fruit><Type>Banana</Type><Amount>14</Amount></Fruit>
</Container>
<Container>
<Country>USA</Country>
<Fruit><Type>Peach</Type><Amount>15</Amount></Fruit>
</Container>
</Containers>
</Factory>

Basically I want to walk through this list and sum the different
fruits. I.e, the result from this list would be...
Apple 23
Banana 26
Peach 15

I know that a container contains different types, i.e.
/Factory/Containers/Container/Fruit/Type and that based on this type,
I want to sum the different
/Factory/Containers/Container/Fruit/Amounts, but I'm not sure how to
write the XSL.

Any help would be greatly appreciated.
--Jim

 
Reply With Quote
 
 
 
 
Joris Gillis
Guest
Posts: n/a
 
      09-22-2005
Tempore 19:41:19, die Thursday 22 September 2005 AD, hinc in foro {comp.text.xml} scripsit <(E-Mail Removed)>:

> Basically I want to walk through this list and sum the different
> fruits. I.e, the result from this list would be...
> Apple 23
> Banana 26
> Peach 15


This is a standarg grouping problem:

<xsl:stylesheet version="1.0"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xslutput method="text"/>

<xsl:key name="fruit" match="Fruit" use="Type"/>

<xsl:template match="Factory">
<xsl:for-each select="Containers/Container/Fruit[generate-id()=generate-id(key('fruit',Type)[1])]">
<xsl:value-of select="Type"/>
<xsl:text> </xsl:text>
<xsl:value-of select="sum(key('fruit',Type)/Amount)"/>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

regards,
--
Joris Gillis (http://users.telenet.be/root-jg/me.html)
«Error, keyboard not found— press F1 to continue» , BIOS
 
Reply With Quote
 
 
 
 
shockride@gmail.com
Guest
Posts: n/a
 
      09-22-2005
Thank you very much. I think that the problem came from not
understanding keys completly and putting the key definition inside of
the template.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
groupby - summing multiple columns in a list of lists Jackson Python 1 05-17-2011 06:24 PM
Summing a 2D list Mark Python 27 06-14-2008 09:15 PM
XSLT sorting / merging / summing david.paik@huscointl.com XML 0 09-20-2006 02:12 PM
Microbenchmark: Summing over array of doubles Yaroslav Bulatov Python 9 08-03-2004 12:36 AM
Counting and summing elements in an array? Help! Alf P. Steinbach C++ 2 02-05-2004 09:27 AM



Advertisments