Summing a list

Discussion in 'XML' started by shockride@gmail.com, Sep 22, 2005.

  1. Guest

    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, 2005
    #1
    1. Advertising

  2. Joris Gillis Guest

    Tempore 19:41:19, die Thursday 22 September 2005 AD, hinc in foro {comp.text.xml} scripsit <>:

    > 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,
    --
    Joris Gillis (http://users.telenet.be/root-jg/me.html)
    «Error, keyboard not found— press F1 to continue» , BIOS
     
    Joris Gillis, Sep 22, 2005
    #2
    1. Advertising

  3. Guest

    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, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alf P. Steinbach
    Replies:
    2
    Views:
    535
    Chris Theis
    Feb 5, 2004
  2. Yaroslav Bulatov

    Microbenchmark: Summing over array of doubles

    Yaroslav Bulatov, Aug 1, 2004, in forum: Python
    Replies:
    9
    Views:
    397
    Christopher T King
    Aug 3, 2004
  3. Replies:
    0
    Views:
    517
  4. Mark

    Summing a 2D list

    Mark, Jun 12, 2008, in forum: Python
    Replies:
    27
    Views:
    732
  5. Jackson
    Replies:
    1
    Views:
    201
    Peter Otten
    May 17, 2011
Loading...

Share This Page