Summary of Unique Records in XML

Discussion in 'XML' started by Gadrin77, May 12, 2004.

  1. Gadrin77

    Gadrin77 Guest

    I can create a list of unique items and display them, but
    I'd like to place a sum of their @Value after each item.

    MS XML 4 SP2 and using XSL (using the Meunchian method).

    XML looks like:

    <Bonus>
    <BonusItem Name="Category One" Value="1"/>
    <BonusItem Name="Category One" Value="1"/>
    <BonusItem Name="Category Two" Value="2"/>
    <BonusItem Name="Category One" Value="1"/>
    <BonusItem Name="Category Two" Value="2"/>
    <BonusItem Name="Category Six" Value="6"/>
    </Bonus>

    and the output should look like:

    Category One: 3
    Category Two: 4
    Category Six: 6

    I've tried sum(@Value) inside a for-each but it doesn't give
    me the total.

    Thanks for your help.
     
    Gadrin77, May 12, 2004
    #1
    1. Advertising

  2. Hello, Gadrin77!
    You wrote on 12 May 2004 15:52:21 -0700:


    [Sorry, skipped]

    You can use the following code snippet:
    [xslt]
    <xsl:template match="Bonus">
    <xsl:apply-templates select="BonusItem[not(@Name =
    preceding-sibling::BonusItem/@Name)]"/>
    </xsl:template>

    <xsl:template match="BonusItem">
    <xsl:element name="{@Name}">
    <xsl:value-of select="sum(//Bonus/BonusItem[@Name =
    current()/@Name]/@Value)" />
    </xsl:element>
    </xsl:template>
    [/xslt]

    Could you provide your code what uses Meunchian method? I don't uderstand
    the problem with the sum function.

    With best regards, Alex Shirshov.
     
    Alex Shirshov, May 13, 2004
    #2
    1. Advertising

  3. Gadrin77

    Gadrin77 Guest

    "Alex Shirshov" <> wrote in message news:<c7v3ae$npl$>...
    > Hello, Gadrin77!
    > You wrote on 12 May 2004 15:52:21 -0700:
    >
    >
    > [Sorry, skipped]
    >
    > You can use the following code snippet:
    > [xslt]
    > <xsl:template match="Bonus">
    > <xsl:apply-templates select="BonusItem[not(@Name =
    > preceding-sibling::BonusItem/@Name)]"/>
    > </xsl:template>
    >
    > <xsl:template match="BonusItem">
    > <xsl:element name="{@Name}">
    > <xsl:value-of select="sum(//Bonus/BonusItem[@Name =
    > current()/@Name]/@Value)" />
    > </xsl:element>
    > </xsl:template>
    > [/xslt]
    >
    > Could you provide your code what uses Meunchian method? I don't uderstand
    > the problem with the sum function.
    >
    > With best regards, Alex Shirshov.


    thanks Alex,

    here's the current incarnation (which I've edited and re-edited
    several
    times).

    I'm still new to XSL and forgot all about Current()

    <?xml version='1.0'?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:key name="BonusCategory" match="BonusItem" use="@Name"/>

    <xsl:template match="/*">
    <xsl:for-each select="/Bonus/BonusItem[count(. |
    key('BonusCategory', @Name)[1])=1]">
    <!-- <xsl:sort select="@Name" order="ascending"/> -->
    <xsl:value-of select="@Name"/>
    <xsl:text>:</xsl:text><xsl:text> </xsl:text>
    <xsl:value-of select="@Value"/>

    <br/>
    </xsl:for-each>

    </xsl:template>

    </xsl:stylesheet>
     
    Gadrin77, May 13, 2004
    #3
  4. Hello, Gadrin77!
    You wrote on 13 May 2004 09:27:22 -0700:


    [Sorry, skipped]

    G> thanks Alex,

    G> here's the current incarnation (which I've edited and re-edited
    G> several
    G> times).

    G> I'm still new to XSL and forgot all about Current()

    G> <?xml version='1.0'?>
    G> <xsl:stylesheet version="1.0"
    G> xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    G> <xsl:key name="BonusCategory" match="BonusItem" use="@Name"/>

    G> <xsl:template match="/*">
    G> <xsl:for-each select="/Bonus/BonusItem[count(. |
    G> key('BonusCategory', @Name)[1])=1]">
    G> <!-- <xsl:sort select="@Name" order="ascending"/> -->
    G> <xsl:value-of select="@Name"/>
    G> <xsl:text>:</xsl:text><xsl:text> </xsl:text>
    G> <xsl:value-of select="@Value"/>

    G> <br/>
    G> </xsl:for-each>

    G> </xsl:template>

    Here you just get value of the first BonusItem in the group. If you want to
    get sum of all values in corresponding group of items, the code will looks
    like:

    <xsl:value-of select="@Name"/>
    <xsl:text>: </xsl:text>
    <xsl:value-of select="sum(key('BonusCategory', @Name)/@Value)" />
    <br/>

    With best regards, Alex Shirshov.
     
    Alex Shirshov, May 14, 2004
    #4
    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. Gadrin77

    Unique Records from Unions

    Gadrin77, May 13, 2004, in forum: XML
    Replies:
    2
    Views:
    451
    Gadrin77
    May 18, 2004
  2. Gadrin77

    Grouping and Unique Records

    Gadrin77, May 20, 2004, in forum: XML
    Replies:
    1
    Views:
    376
    Zdenek Oklestek
    May 21, 2004
  3. ToshiBoy
    Replies:
    6
    Views:
    850
    ToshiBoy
    Aug 12, 2008
  4. J.D. Buehls
    Replies:
    0
    Views:
    118
    J.D. Buehls
    Jun 18, 2004
  5. Token Type
    Replies:
    9
    Views:
    359
    Chris Angelico
    Sep 9, 2012
Loading...

Share This Page