473,395 Members | 1,554 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,395 software developers and data experts.

Simple aggregation using xquery

I dont know xquery , so i was thinking someone in the forum will help
me out with this easy question

Suppose I have an instance document given below
<Root>
<Author Name='Alice' NumberOfBooks = '1'>
<Author Name='Bob' NumberOfBooks = '4'>
<Author Name='Collin' NumberOfBooks = '3'>
<Author Name='David' NumberOfBooks = '2'>
</Root>

Is there a way i can get the total number of books published by all
authors using a single xquery statement. (similiar to say SQL - select
sum(col) semantics)

What i DO NOT WANT to do is gettting all the nodes and doing the sum
at the client/my application. I would rather have the XML engine handle
the aggregation
Suma

Jul 24 '06 #1
4 2465
SL
"Suma" a écrit :
I dont know xquery , so i was thinking someone in the forum will help
me out with this easy question

Suppose I have an instance document given below
<Root>
<Author Name='Alice' NumberOfBooks = '1'>
<Author Name='Bob' NumberOfBooks = '4'>
<Author Name='Collin' NumberOfBooks = '3'>
<Author Name='David' NumberOfBooks = '2'>
</Root>

Is there a way i can get the total number of books published by all
authors using a single xquery statement. (similiar to say SQL - select
sum(col) semantics)
sum(/Root/Author/@NumberOfBookds)
Jul 24 '06 #2
Great thanks
Now what if i need to do a aggregate over groups?
My repro was simpler than what i intend to do
<Root>
<Author ="Alice" Attrib="1">
<Author ="Alice" Attrib="2">
<Author ="Bob" Attrib="3">
<Author ="Bob" Attrib="4">
</Root>

I need to return
Alice 3
Bob 7
and
so on
Suma
SL wrote:
"Suma" a écrit :
I dont know xquery , so i was thinking someone in the forum will help
me out with this easy question

Suppose I have an instance document given below
<Root>
<Author Name='Alice' NumberOfBooks = '1'>
<Author Name='Bob' NumberOfBooks = '4'>
<Author Name='Collin' NumberOfBooks = '3'>
<Author Name='David' NumberOfBooks = '2'>
</Root>

Is there a way i can get the total number of books published by all
authors using a single xquery statement. (similiar to say SQL - select
sum(col) semantics)
sum(/Root/Author/@NumberOfBookds)
Jul 24 '06 #3
SL
"Suma" a écrit :
Great thanks
Now what if i need to do a aggregate over groups?
My repro was simpler than what i intend to do
<Root>
<Author ="Alice" Attrib="1">
<Author ="Alice" Attrib="2">
<Author ="Bob" Attrib="3">
<Author ="Bob" Attrib="4">
^^^
This is not well-formed XML. I assume you have:
<Author name="Bob" Attrib="4">
^^^^
instead
</Root>
I need to return
Alice 3
Bob 7
and
so on
Suma
It is an XPath issue :

sum(/Root/Author[@name='Alice']/@Attrib)

If you want to loop over all possible names, XQuery provide many
functionnalities, such as the so-called "Flower expression":

for $author in Book/Author/@name
return
<nbrOfBooks>{sum(/Root/Author[@name=$author]/@Attrib)}</nbrOfBooks>

(I havn't practiced XQuery for some time, and this is not tested!)
Jul 24 '06 #4
SL

Sorry:

- for $author in Book/Author/@name
+ for $author in /Book/Author/@name
return
- <nbrOfBooks>{sum(/Root/Author[@name=$author]/@Attrib)}</nbrOfBooks>
+ <nbrOfBooks>{sum(/Book/Author[@name=$author]/@Attrib)}</nbrOfBooks>
Jul 24 '06 #5

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

Similar topics

2
by: Tom Corcoran | last post by:
I am working to ease updating of a html page by transforming 2 xml files. I was going to use xslt for this and had bought 2 unopened books, wrox xslt and o'reilly's xslt cookbook. But am now...
0
by: Benjamin G. Jones | last post by:
I am having a very basic problem with XQuery. I want to use an XQuery API in Java (either Saxon or Qexo), and I have an XQuery expression that works as expeced from the command line if I specify...
8
by: Clamps | last post by:
So I've been reading about XQuery, but cannot find a dll or namespace download to use the technology in vs.net 2002 or 2003. Any directions would be great. -- "If I'm the president, we're going...
5
by: John Wood | last post by:
Let's say you're provided with an instance of a class. The instantiation takes place in another module that you have no control over. However, you've extended that class with your own value-added...
6
by: Chua Wen Ching | last post by:
Hi there, I had this xml file with me (not yet consider implementing xml namespaces yet). <?xml version='1.0'?> <Object> <Windows> <EID>1</EID> <EDesc>Error 1</EDesc> </Windows>
23
by: SenthilVel | last post by:
Hi Can any one let me know the websites/Pdf for learning Aggragation in C#?? Thanks Senthil
2
by: tammo.mueller | last post by:
Hi, I'm trying to execute a xquery statement and I get always errors. It seems that only the prolog section is giving me errors. If I want to declare a namespace the xquery processor is coming...
6
by: Armel Asselin | last post by:
Hello, I'm searching for a simple command line tool to manipulate XML files. The idea would be commands such as that: xmanip-tool set /document/xpath/@name="value" remove //wrong-nodes add...
6
by: Jeff | last post by:
hey Can OO Aggregation be described as: - A system of objects that are built using each other any comments? Jeff
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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...
0
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,...
0
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...
0
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...

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.