473,378 Members | 1,207 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Grouping and Sum problem

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
Jul 20 '05 #1
3 3244
Use:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output 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)" <sp******@berzerker-soft.com> wrote in message
news:va********************************@4ax.com...
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

Jul 20 '05 #2
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"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output 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)" <sp******@berzerker-soft.com> wrote in message
news:va********************************@4ax.com...
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

Jul 20 '05 #3
On Tue, 30 Sep 2003 22:50:55 +0100, "Marrow"
<marrow-NO-@-SPAM-marrowsoft.com> 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
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Debbie Davis | last post by:
Hi there, SQL 2000 I have the following query: SELECT sponsor, COUNT(sponsor) * 2 AS total FROM Referrals GROUP BY sponsor Works great, returns the sponsor and the total * 2 of their...
5
by: Jody Greening | last post by:
Transforming with XSLT, Grouping elements until difference found. I am seeking some help with the following problem, I am fairly new at XSLT transformations, and my problem may lie in looking at...
2
by: Andreas Håkansson | last post by:
Seeing how my previous post seem to have fallen between the cracks, I thought I would have a second, more direct, go at it. So my question is "Is it possible to group (Muenchian method) over...
1
by: Brian Coy | last post by:
I am creating a database to track scrap on a daily basis at my plant. I have to provide a weekly scrap report with the amount of each part scrapped per day. I have the basic database set up, and...
3
by: ahaque38 | last post by:
Hello. Using A2K SP3, I am having the following problem with a report using "Sorting and Grouping". I have recently added a grouping in the reports for "Category2<>'CONTRACTS'". I have...
8
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At...
0
by: virendra | last post by:
hi, I am working on crystal report 9 in asp.net. I created multilevel dynamic grouping in CR. if page is load firsttime. and i am giving firstlevel of grouping or whatever it's working fine. but...
0
by: Corey | last post by:
hello, I’m trying to run a query and I’m getting error messages Can anyone help me get though this problem? 1: Tried this and got this error message ORA-00923 , decode...
4
by: Chris | last post by:
I tried to retrieve the digit grouping symbol in MSAccess but unfortunately 3;0 is retrieved instead of comma which is my symbol. Function retrieves decimal symbol and list separator without any...
6
patjones
by: patjones | last post by:
Good afternoon: This seems like it shouldn't be hard, and then again this is how so many problems seem at the outset. My situation is this: I have a report called rptMain319, which is based...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.