Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Summary of Unique Records in XML

Reply
Thread Tools

Summary of Unique Records in XML

 
 
Gadrin77
Guest
Posts: n/a
 
      05-12-2004
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.
 
Reply With Quote
 
 
 
 
Alex Shirshov
Guest
Posts: n/a
 
      05-13-2004
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.


 
Reply With Quote
 
 
 
 
Gadrin77
Guest
Posts: n/a
 
      05-13-2004
"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"
xmlnssl="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>
 
Reply With Quote
 
Alex Shirshov
Guest
Posts: n/a
 
      05-14-2004
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> xmlnssl="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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a unique method in python to unique a list? Token Type Python 9 09-09-2012 02:13 PM
list question... unique values in all possible unique spots ToshiBoy Python 6 08-12-2008 05:01 AM
INSERT multiple records from checkbox form with unique ids J.D. Buehls ASP General 0 06-18-2004 03:47 PM
Grouping and Unique Records Gadrin77 XML 1 05-21-2004 06:21 AM
Unique Records from Unions Gadrin77 XML 2 05-18-2004 02:05 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57