Summing a list

S

shockride

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
 
J

Joris Gillis

Tempore 19:41:19 said:
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"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput 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,
 
S

shockride

Thank you very much. I think that the problem came from not
understanding keys completly and putting the key definition inside of
the template.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,000
Messages
2,570,252
Members
46,848
Latest member
CristineKo

Latest Threads

Top