Help with recursive sum

Discussion in 'XML' started by The alMIGHTY N, Dec 27, 2006.

  1. I've been having some issues with recursive summing and have been
    unsuccessful at getting some of the solutions I've seen online to work.
    I'm hoping that perhaps somebody in the newsgroup will be able to shed
    some light on the issue.

    I have the following XML:

    <departmentStore>
    <name>S-Mart</name>
    <department>
    <name>Electronics</name>
    <section>
    <name>Video Games</name>
    <system>
    <name>Xbox 360</name>
    <manufacturer>Microsoft</manufacturer>
    <price>$399.99</price>
    <gameLibrary>
    <game>
    <name>Call of Duty 3</name>
    <genre>Shooter</genre>
    <publisher>Activision</publisher>
    <developer>Treyarch</developer>
    <price>$59.99</price>
    </game>
    <game>
    <name>Fight Night Round 3</name>
    <genre>Sports</genre>
    <publisher>Electronic Arts</publisher>
    <developer>EA Chicago</developer>
    <price>$59.99</price>
    </game>
    <game>
    <name>Rainbow Six Vegas</name>
    <genre>Shooter</genre>
    <publisher>Ubisoft</publisher>
    <developer>Ubisoft Montreal</developer>
    <price>$59.99</price>
    </game>
    <game>
    <name>Viva Pinata</name>
    <genre>Simulation</genre>
    <publisher>Microsoft</publisher>
    <developer>Rare</developer>
    <price>$49.99</price>
    </game>
    </gameLibrary>
    </system>
    </section>
    </department>
    </departmentStore>

    departmentStore, department, section, system, and gameLibrary all have
    more children nodes than what are shown.

    What I want to do is get the sum of all video game prices for a
    specific system and display it when applying the template that matches
    "system."

    Any help would be very much appreciated. Thanks!

    Nathaniel
    The alMIGHTY N, Dec 27, 2006
    #1
    1. Advertising

  2. The alMIGHTY N

    Peter Flynn Guest

    The alMIGHTY N wrote:
    > I've been having some issues with recursive summing and have been
    > unsuccessful at getting some of the solutions I've seen online to work.
    > I'm hoping that perhaps somebody in the newsgroup will be able to shed
    > some light on the issue.
    >
    > I have the following XML:
    >
    > <departmentStore>
    > <name>S-Mart</name>
    > <department>
    > <name>Electronics</name>
    > <section>
    > <name>Video Games</name>
    > <system>
    > <name>Xbox 360</name>
    > <manufacturer>Microsoft</manufacturer>
    > <price>$399.99</price>
    > <gameLibrary>
    > <game>
    > <name>Call of Duty 3</name>
    > <genre>Shooter</genre>
    > <publisher>Activision</publisher>
    > <developer>Treyarch</developer>
    > <price>$59.99</price>
    > </game>
    > <game>
    > <name>Fight Night Round 3</name>
    > <genre>Sports</genre>
    > <publisher>Electronic Arts</publisher>
    > <developer>EA Chicago</developer>
    > <price>$59.99</price>
    > </game>
    > <game>
    > <name>Rainbow Six Vegas</name>
    > <genre>Shooter</genre>
    > <publisher>Ubisoft</publisher>
    > <developer>Ubisoft Montreal</developer>
    > <price>$59.99</price>
    > </game>
    > <game>
    > <name>Viva Pinata</name>
    > <genre>Simulation</genre>
    > <publisher>Microsoft</publisher>
    > <developer>Rare</developer>
    > <price>$49.99</price>
    > </game>
    > </gameLibrary>
    > </system>
    > </section>
    > </department>
    > </departmentStore>
    >
    > departmentStore, department, section, system, and gameLibrary all have
    > more children nodes than what are shown.
    >
    > What I want to do is get the sum of all video game prices for a
    > specific system and display it when applying the template that matches
    > "system."


    Never store prices with the currency sign embedded, because they then
    cease to be numbers and can't be manipulated easily in functions. Always
    store the currency as an attribute of the price, using the standard
    3-letter currency codes used by your bank (eg here possibly USD or CAD),
    like <price currency="USD">49.99</price>
    and only translate to a currency sign for purposes of display or print.
    Then it's easy to write:

    <xsl:template match="system">
    <xsl:value-of select="sum(gameLibrary/game/price)"/>
    </xsl:template>

    If the data is not yours (ie you get given it), either persuade the
    originators to do it right, or pass it through a filter which removes
    the currency symbol or rewrites the document to use an attribute.

    Motto: get the data model right to begin with, and everything else
    pretty much falls into place. Get the data model wrong, and your project
    is hosed before you start.

    ///Peter
    --
    XML FAQ: http://xml.silmaril.ie/
    Peter Flynn, Dec 27, 2006
    #2
    1. Advertising

  3. This can easily be done with FXSL.

    Search for:

    transform-and-sum


    Cheers,
    Dimitre Novatchev



    "The alMIGHTY N" <> wrote in message
    news:...
    > I've been having some issues with recursive summing and have been
    > unsuccessful at getting some of the solutions I've seen online to work.
    > I'm hoping that perhaps somebody in the newsgroup will be able to shed
    > some light on the issue.
    >
    > I have the following XML:
    >
    > <departmentStore>
    > <name>S-Mart</name>
    > <department>
    > <name>Electronics</name>
    > <section>
    > <name>Video Games</name>
    > <system>
    > <name>Xbox 360</name>
    > <manufacturer>Microsoft</manufacturer>
    > <price>$399.99</price>
    > <gameLibrary>
    > <game>
    > <name>Call of Duty 3</name>
    > <genre>Shooter</genre>
    > <publisher>Activision</publisher>
    > <developer>Treyarch</developer>
    > <price>$59.99</price>
    > </game>
    > <game>
    > <name>Fight Night Round 3</name>
    > <genre>Sports</genre>
    > <publisher>Electronic Arts</publisher>
    > <developer>EA Chicago</developer>
    > <price>$59.99</price>
    > </game>
    > <game>
    > <name>Rainbow Six Vegas</name>
    > <genre>Shooter</genre>
    > <publisher>Ubisoft</publisher>
    > <developer>Ubisoft Montreal</developer>
    > <price>$59.99</price>
    > </game>
    > <game>
    > <name>Viva Pinata</name>
    > <genre>Simulation</genre>
    > <publisher>Microsoft</publisher>
    > <developer>Rare</developer>
    > <price>$49.99</price>
    > </game>
    > </gameLibrary>
    > </system>
    > </section>
    > </department>
    > </departmentStore>
    >
    > departmentStore, department, section, system, and gameLibrary all have
    > more children nodes than what are shown.
    >
    > What I want to do is get the sum of all video game prices for a
    > specific system and display it when applying the template that matches
    > "system."
    >
    > Any help would be very much appreciated. Thanks!
    >
    > Nathaniel
    >
    Dimitre Novatchev, Dec 28, 2006
    #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. un[real]
    Replies:
    6
    Views:
    284
    Gaijinco
    Nov 3, 2005
  2. n00m
    Replies:
    12
    Views:
    1,114
  3. vamsi
    Replies:
    21
    Views:
    2,076
    Keith Thompson
    Mar 9, 2009
  4. Maggie

    Help with cumulative sum

    Maggie, Sep 8, 2009, in forum: Python
    Replies:
    13
    Views:
    615
    Andreas Waldenburger
    Sep 10, 2009
  5. Russel Walker
    Replies:
    9
    Views:
    114
    Russel Walker
    Jul 7, 2013
Loading...

Share This Page