Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Grouping and Sum problem

Reply
Thread Tools

Grouping and Sum problem

 
 
Bryce (Work)
Guest
Posts: n/a
 
      09-30-2003
Here is the xml I'm working with:

<result>
<EFORM>
<testNode>
<section1>
<string1>ADAM</string1>
<integer1>55</integer1>
</section1>
</testNode>
</EFORM>
<EFORM>
<testNode>
<section1>
<string1>JOHN</string1>
<integer1>43</integer1>
</section1>
</testNode>
</EFORM>
<EFORM>
<testNode>
<section1>
<string1>ADAM</string1>
<integer1>22</integer1>
</section1>
</testNode>
</EFORM>
<EFORM>
<testNode>
<section1>
<string1>JOHN</string1>
<integer1>15</integer1>
</section1>
</testNode>
</EFORM>
</result>

What I'm trying to do is group by <string1> and sum <integer1>

for example:
JOHN 58
ADAM 77

Using Muenchian method, I can count the number of nodes for each
group, but am having difficulty getting the sum. Since I don't 100%
understand the code to do the grouping, I'm having difficutly. Here's
what I have for Counting:

<xslout:key name="keyname" match="EFORM"
use="testNode/Section1/string1" />

<xslout:template match="/">
<summary>
<xslout:for-each select="//EFORM[count(. | key('keyname',
testNode/Section1/string1)[1]) = 1]">
<xslout:sort select="testNode/Section1/string1"/>
<summaryGroup>
<summaryField>
<xslout:value-of select="testNode/Section1/string1"/>
</summaryField>
<summaryvalue>
<xslout:value-of select="count(key('keyname',
testNode/Section1/string1))"/>
</summaryvalue>
</summaryGroup>
</xslout:for-each>
</summary>
</xslout:template>

I'm not sure what I shoudl put in to do the sum... Or even if I can do
it this way.

Any help is appreciated.

Bryce
 
Reply With Quote
 
 
 
 
Dimitre Novatchev
Guest
Posts: n/a
 
      09-30-2003
Use:

<xsl:stylesheet version="1.0"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xslutput method="text"/>

<xsl:key name="kStr" match="string1" use="."/>

<xsl:template match="/">
<xsl:for-each
select="/*/*/*/*/string1
[generate-id()
=
generate-id(key('kStr', .)[1])
]">
<xsl:value-of
select="concat('&#xA;', ., ' ',
sum(key('kStr', .)/../integer1)
)"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

When this transformation is applied on your source.xml, the wanted result is
produced:

ADAM 77
JOHN 58


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL


"Bryce (Work)" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Here is the xml I'm working with:
>
> <result>
> <EFORM>
> <testNode>
> <section1>
> <string1>ADAM</string1>
> <integer1>55</integer1>
> </section1>
> </testNode>
> </EFORM>
> <EFORM>
> <testNode>
> <section1>
> <string1>JOHN</string1>
> <integer1>43</integer1>
> </section1>
> </testNode>
> </EFORM>
> <EFORM>
> <testNode>
> <section1>
> <string1>ADAM</string1>
> <integer1>22</integer1>
> </section1>
> </testNode>
> </EFORM>
> <EFORM>
> <testNode>
> <section1>
> <string1>JOHN</string1>
> <integer1>15</integer1>
> </section1>
> </testNode>
> </EFORM>
> </result>
>
> What I'm trying to do is group by <string1> and sum <integer1>
>
> for example:
> JOHN 58
> ADAM 77
>
> Using Muenchian method, I can count the number of nodes for each
> group, but am having difficulty getting the sum. Since I don't 100%
> understand the code to do the grouping, I'm having difficutly. Here's
> what I have for Counting:
>
> <xslout:key name="keyname" match="EFORM"
> use="testNode/Section1/string1" />
>
> <xslout:template match="/">
> <summary>
> <xslout:for-each select="//EFORM[count(. | key('keyname',
> testNode/Section1/string1)[1]) = 1]">
> <xslout:sort select="testNode/Section1/string1"/>
> <summaryGroup>
> <summaryField>
> <xslout:value-of select="testNode/Section1/string1"/>
> </summaryField>
> <summaryvalue>
> <xslout:value-of select="count(key('keyname',
> testNode/Section1/string1))"/>
> </summaryvalue>
> </summaryGroup>
> </xslout:for-each>
> </summary>
> </xslout:template>
>
> I'm not sure what I shoudl put in to do the sum... Or even if I can do
> it this way.
>
> Any help is appreciated.
>
> Bryce



 
Reply With Quote
 
 
 
 
Marrow
Guest
Posts: n/a
 
      09-30-2003
Hi Bryce,

The case in some of your XPath expressions may be causing some problems (you
have "Section1" in places where you probably want "section1" - as per the
input XML).

This XSL works...

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xslutput method="xml" indent="yes"/>
<xsl:key name="keyname" match="EFORM" use="testNode/section1/string1" />
<xsl:template match="/">
<summary>
<xsl:for-each select="result/EFORM[generate-id() =
generate-id(key('keyname',testNode/section1/string1))]">
<xsl:sort select="testNode/section1/string1"/>
<summaryGroup>
<summaryField>
<xsl:value-of select="testNode/section1/string1"/>
</summaryField>
<summaryvalue>
<xsl:value-of
select="sum(key('keyname',testNode/section1/string1)/testNode/section1/integ
er1)"/>
</summaryvalue>
</summaryGroup>
</xsl:for-each>
</summary>
</xsl:template>
</xsl:stylesheet>

Hope this helps
Marrow
http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
http://www.topxml.com/Xselerator


"Bryce (Work)" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Here is the xml I'm working with:
>
> <result>
> <EFORM>
> <testNode>
> <section1>
> <string1>ADAM</string1>
> <integer1>55</integer1>
> </section1>
> </testNode>
> </EFORM>
> <EFORM>
> <testNode>
> <section1>
> <string1>JOHN</string1>
> <integer1>43</integer1>
> </section1>
> </testNode>
> </EFORM>
> <EFORM>
> <testNode>
> <section1>
> <string1>ADAM</string1>
> <integer1>22</integer1>
> </section1>
> </testNode>
> </EFORM>
> <EFORM>
> <testNode>
> <section1>
> <string1>JOHN</string1>
> <integer1>15</integer1>
> </section1>
> </testNode>
> </EFORM>
> </result>
>
> What I'm trying to do is group by <string1> and sum <integer1>
>
> for example:
> JOHN 58
> ADAM 77
>
> Using Muenchian method, I can count the number of nodes for each
> group, but am having difficulty getting the sum. Since I don't 100%
> understand the code to do the grouping, I'm having difficutly. Here's
> what I have for Counting:
>
> <xslout:key name="keyname" match="EFORM"
> use="testNode/Section1/string1" />
>
> <xslout:template match="/">
> <summary>
> <xslout:for-each select="//EFORM[count(. | key('keyname',
> testNode/Section1/string1)[1]) = 1]">
> <xslout:sort select="testNode/Section1/string1"/>
> <summaryGroup>
> <summaryField>
> <xslout:value-of select="testNode/Section1/string1"/>
> </summaryField>
> <summaryvalue>
> <xslout:value-of select="count(key('keyname',
> testNode/Section1/string1))"/>
> </summaryvalue>
> </summaryGroup>
> </xslout:for-each>
> </summary>
> </xslout:template>
>
> I'm not sure what I shoudl put in to do the sum... Or even if I can do
> it this way.
>
> Any help is appreciated.
>
> Bryce



 
Reply With Quote
 
Bryce (Work)
Guest
Posts: n/a
 
      10-01-2003
On Tue, 30 Sep 2003 22:50:55 +0100, "Marrow"
<(E-Mail Removed)> wrote:

>Hi Bryce,
>

[snip]
>Hope this helps
>Marrow
>http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
>http://www.topxml.com/Xselerator


Yes indeed. And I even figured out why it works I just had to
figure out where the root was, and XPath to th at...

Thanks to everyone for their help
 
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
Grouping & Sum of result page David ASP General 5 09-25-2006 04:06 PM
XML - problem with sum function cieron XML 2 05-16-2005 09:21 PM
XQuery problem - fn:sum sjurisic@mrxsolutions.com XML 0 03-17-2005 05:56 PM
Problem regarding sum of digits of floating point number in C Shreyas Kulkarni C Programming 1 12-27-2004 03:40 AM
Port Grouping and Trunking problem hari Cisco 7 01-07-2004 09:01 AM



Advertisments