473,386 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Summing a list

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

Sep 22 '05 #1
2 1180
Tempore 19:41:19, die Thursday 22 September 2005 AD, hinc in foro {comp.text.xml} scripsit <sh*******@gmail.com>:
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:output 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
Sep 22 '05 #2
Thank you very much. I think that the problem came from not
understanding keys completly and putting the key definition inside of
the template.

Sep 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Yaroslav Bulatov | last post by:
I made an array of 10 million floats timed how long it takes to sum the elements, here's what I got (millis): gcc -O2: 21 Python with numarray: 104 Python with Numeric: 302...
1
by: Ciar?n | last post by:
I am trying to check a list (MyList) against another List (SupplierList). I want sum the Qty's of UniqueID on MyList and extract the sum of the same UniqueId's on SupplierList. BTW There are more...
2
by: SunMan | last post by:
Hello! I am trying to create a program that will ask for a user to enter a series of letters (codes) and then print out a table that shows the codes in decending frequency. Only letters will be...
2
by: paedrow | last post by:
I'm trying to apply some XSL tranformations to an XML file generated from a Sharepoint List. Here is a sample XML: <?xml version="1.0" encoding="utf-8" ?> <listitems...
4
by: dancole42 | last post by:
So I have an invoicing database based on two main forms: Orders and OrderLines. Orders has fields like: OrderID BillingMethod OrderDate CreditCard CCExp OrdSubTotal ShippingCharge
7
by: lethek39 | last post by:
Hey I have been trying to figure out how to sum rows and columns in a matrix square. I also have been trying to get the program to list the numbers of the diagonal in the matrix. So far this is the...
1
by: mjta | last post by:
Can I sum the values from a list box. If I use cascading list boxes, can I sum the values in a text box automatically when the list is updated?
27
by: Mark | last post by:
Hi all, I have a scenario where I have a list like this: User Score 1 0 1 1 1 5 2 3 2 1
9
by: beginner | last post by:
Hi All, I have a list of records like below: rec= Now I want to write code to find out the ratio of the sums of the two fields. One thing I can do is:
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.