473,794 Members | 2,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:templat e match="/">
<summary>
<xslout:for-each select="//EFORM[count(. | key('keyname',
testNode/Section1/string1)[1]) = 1]">
<xslout:sort select="testNod e/Section1/string1"/>
<summaryGroup >
<summaryField >
<xslout:value-of select="testNod e/Section1/string1"/>
</summaryField>
<summaryvalue >
<xslout:value-of select="count(k ey('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 3261
Use:

<xsl:styleshe et 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******@berze rker-soft.com> wrote in message
news:va******** *************** *********@4ax.c om...
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:templat e match="/">
<summary>
<xslout:for-each select="//EFORM[count(. | key('keyname',
testNode/Section1/string1)[1]) = 1]">
<xslout:sort select="testNod e/Section1/string1"/>
<summaryGroup >
<summaryField >
<xslout:value-of select="testNod e/Section1/string1"/>
</summaryField>
<summaryvalue >
<xslout:value-of select="count(k ey('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:styleshe et 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="testNod e/section1/string1"/>
<summaryGroup >
<summaryField >
<xsl:value-of select="testNod e/section1/string1"/>
</summaryField>
<summaryvalue >
<xsl:value-of
select="sum(key ('keyname',test Node/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******@berze rker-soft.com> wrote in message
news:va******** *************** *********@4ax.c om...
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:templat e match="/">
<summary>
<xslout:for-each select="//EFORM[count(. | key('keyname',
testNode/Section1/string1)[1]) = 1]">
<xslout:sort select="testNod e/Section1/string1"/>
<summaryGroup >
<summaryField >
<xslout:value-of select="testNod e/Section1/string1"/>
</summaryField>
<summaryvalue >
<xslout:value-of select="count(k ey('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
1878
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 referrals
5
2178
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 it from a traditional programming point of view. I have did quite a bit of searching but have found no answers to my particular problem... please read below XML for the problem I am having.
2
1704
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 multiple nodes?" I will use an example to try to explain what I need to do and what I have for data. The example might not be very realistic but it's much easier than to try and explain using the scenario I have =P Suppose I had a list of...
1
2081
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 am trying to generate a report. I have created a report that will show all the data. But I need to show either an empty group for the data on days that there is no scrap or a comment relating as much. My report is set up with a weekly...
3
2744
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 reports at the plan (overall totals), department and division levels which have sorting and grouping implemented with this new
8
3539
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 the moment the printed output is usually going to Word. It's turning into an unholy mess, because I'm having to prepare umpteen different Word templates, and the queries that drive them, depending on what events a course has.
0
1414
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 once page is loaded and i am changing the grouping options it's not affecting the page. whatever the previous data in report that only is showing. but it's exporting properly in excel file. So it can be a problem of cache.
0
1226
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 (grouping(dsd.Field1,'RETURNED', COUNT(*), 0)) Net1 , decode (grouping(dsd.field1,'COMPLETED', COUNT(*), 0)) Net2
4
3858
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 problem. I used the code below: var_char_copied = GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SGROUPING, var_SYSTEM_DTG, Len(var_SYSTEM_DTG))
6
1650
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 on a table tblMain319. The report groups employees by their work location (). By putting a Count(*) function in the footer for the Payroll Distribution grouping, I can get the total number of employees in that particular work location. My problem...
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10435
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10213
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10163
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.