Summary of Unique Records in XML

G

Gadrin77

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.
 
A

Alex Shirshov

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.
 
G

Gadrin77

Alex Shirshov said:
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>
 
A

Alex Shirshov

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top