Discussion:
[xquery-talk] Using Sum and Count
Wei, Alice J.
17 years ago
Permalink
Hi, XQueriers:

I have an XML here as follows:

<project>

<projection><!--More Markup--></projection>
<projection><!--More Markup--></projection>
<!--More markup-->
</project>

My XQuery:

declare boundary-space preserve;

for $project in
doc("http://chausie.slis.indiana.edu:8080/exist/rest//db/resume/resume.xml"),
$head in $project//projection
return
<statistics>sum({count($head)})</statistics>

I intend to get the result of how many items in <projection>, but when I execute it, it only gives me lots and lots of <statistics>sum(1)</statistics>
until the document is finished.

Can any expert out there help me out and tell me what is the matter with my code?

Thanks to those who can help.

Alice.

======================================================
Alice Wei
MIS 2008
School of Library and Information Science
Indiana University Bloomington
***@indiana.edu
Michael Kay
17 years ago
Permalink
When you use an element constructor in XQuery, for example

<A>....</A>

the text between the tags can contain literal text:

<A>Here is some output</A>

or expressions written within curly braces:

<A>The answer is {2+40}</A>

A common mistake is to omit the curly braces. If you write

<A>2+40</A>

the output will not be <A>42</A>, but <A>2+40</A>

You have made this mistake in a rather unusual way here:

<statistics>sum({count($head)})</statistics>

Here sum() is taken as literal text, not as an expression, because it is
outside the curly braces.

However, changing it to

<statistics>{sum(count($head))}</statistics>

would not solve the problem, because count() returns a single number, and
summing a set of numbers that only contains one number doesn't do anything
very useful (though it works). Also, $head is iterating over the items in a
sequence, so count($head) is always one.

Unfortunately though I don't really know what you were trying to compute, so
I can't correct it for you.

Michael Kay
http://www.saxonica.com/


> -----Original Message-----
> From: talk-***@x-query.com
> [mailto:talk-***@x-query.com] On Behalf Of Wei, Alice J.
> Sent: 15 January 2008 22:58
> To: ***@x-query.com
> Subject: [xquery-talk] Using Sum and Count
>
> Hi, XQueriers:
>
> I have an XML here as follows:
>
> <project>
>
> <projection><!--More Markup--></projection>
> <projection><!--More Markup--></projection> <!--More
> markup--> </project>
>
> My XQuery:
>
> declare boundary-space preserve;
>
> for $project in
> doc("http://chausie.slis.indiana.edu:8080/exist/rest//db/resum
> e/resume.xml"),
> $head in $project//projection
> return
> <statistics>sum({count($head)})</statistics>
>
> I intend to get the result of how many items in <projection>,
> but when I execute it, it only gives me lots and lots of
> <statistics>sum(1)</statistics> until the document is finished.
>
> Can any expert out there help me out and tell me what is the
> matter with my code?
>
> Thanks to those who can help.
>
> Alice.
>
> ======================================================
> Alice Wei
> MIS 2008
> School of Library and Information Science Indiana University
> Bloomington ***@indiana.edu
>
> _______________________________________________
> ***@x-query.com
> http://x-query.com/mailman/listinfo/talk
David Carlisle
17 years ago
Permalink
If I understand your requirement you just want
<statistics>{
count(doc("http://chausie.slis.indiana.edu:8080/exist/rest//db/resume/resume.xml")//projection)
}</statistics>
no need for a FLWR clause here.



for $project in
doc("http://chausie.slis.indiana.edu:8080/exist/rest//db/resume/resume.xml"),

using for over a single node is rather odd in xquery (it means the same
as let, it's needed in Xpath2 which lacks the let clause, but not here)

$head in $project//projection


here again I think you intended let rather than for, using for means
that $head gets bound, on each iteration, to a single projection
element, so count($head) is always going to be 1.

Conversely
let $head := $project//projection

would bind $head to the sequence of elements so count() would do
something useful.

finally I'm not sure what you intended to sum with sum() but expressions
need to be in {} as otherwise it's just taken as a text node child of
the generated eleemnt.

David

________________________________________________________________________
The Numerical Algorithms Group Ltd is a company registered in England
and Wales with company number 1249803. The registered office is:
Wilkinson House, Jordan Hill Road, Oxford OX2 8DR, United Kingdom.

This e-mail has been scanned for all viruses by Star. The service is
powered by MessageLabs.
________________________________________________________________________
Wei, Alice J.
17 years ago
Permalink
Hi,

Thanks for the replies. Returning to your question, what I want is something simple. That is, to compute how many "nodes" there are in the XML to express how many child occurrences are from the parent.

The result I have computed before your configurations show individual numbers of the occurrences of the nodes, which is "1". Instead of seeing "1" line after line, I only want to see one line that tells me how many occurrences are there from the XML without printing the rest of the stuff in the code.

Is this possible?

Thanks to those who can help.
======================================================
Alice Wei
MIS 2008
School of Library and Information Science
Indiana University Bloomington
***@indiana.edu
________________________________________
From: David Carlisle [***@nag.co.uk]
Sent: Tuesday, January 15, 2008 6:25 PM
To: Wei, Alice J.
Cc: ***@x-query.com
Subject: Re: [xquery-talk] Using Sum and Count

If I understand your requirement you just want
<statistics>{
count(doc("http://chausie.slis.indiana.edu:8080/exist/rest//db/resume/resume.xml")//projection)
}</statistics>
no need for a FLWR clause here.



for $project in
doc("http://chausie.slis.indiana.edu:8080/exist/rest//db/resume/resume.xml"),

using for over a single node is rather odd in xquery (it means the same
as let, it's needed in Xpath2 which lacks the let clause, but not here)

$head in $project//projection


here again I think you intended let rather than for, using for means
that $head gets bound, on each iteration, to a single projection
element, so count($head) is always going to be 1.

Conversely
let $head := $project//projection

would bind $head to the sequence of elements so count() would do
something useful.

finally I'm not sure what you intended to sum with sum() but expressions
need to be in {} as otherwise it's just taken as a text node child of
the generated eleemnt.

David

________________________________________________________________________
The Numerical Algorithms Group Ltd is a company registered in England
and Wales with company number 1249803. The registered office is:
Wilkinson House, Jordan Hill Road, Oxford OX2 8DR, United Kingdom.

This e-mail has been scanned for all viruses by Star. The service is
powered by MessageLabs.
________________________________________________________________________
David Carlisle
17 years ago
Permalink
> child occurrences are from the parent.
you used // so checking descendents not just children.

> Is this possible?
yes that's what the code I suggested (and identical code from John
Snelson) does.


David

________________________________________________________________________
The Numerical Algorithms Group Ltd is a company registered in England
and Wales with company number 1249803. The registered office is:
Wilkinson House, Jordan Hill Road, Oxford OX2 8DR, United Kingdom.

This e-mail has been scanned for all viruses by Star. The service is
powered by MessageLabs.
________________________________________________________________________
John Snelson
17 years ago
Permalink
Hi Alice,

You are iterating over the nodes in $project//projection, binding each
one in turn to $head. So your query is quite right in telling you that
$head contains 1 item!

You probably want something like this:

count(doc("http://chausie.slis.indiana.edu:8080/exist/rest//db/resume/resume.xml")//projection)

John

Wei, Alice J. wrote:
> Hi, XQueriers:
>
> I have an XML here as follows:
>
> <project>
>
> <projection><!--More Markup--></projection>
> <projection><!--More Markup--></projection>
> <!--More markup-->
> </project>
>
> My XQuery:
>
> declare boundary-space preserve;
>
> for $project in
> doc("http://chausie.slis.indiana.edu:8080/exist/rest//db/resume/resume.xml"),
> $head in $project//projection
> return
> <statistics>sum({count($head)})</statistics>
>
> I intend to get the result of how many items in <projection>, but when I execute it, it only gives me lots and lots of <statistics>sum(1)</statistics>
> until the document is finished.
>
> Can any expert out there help me out and tell me what is the matter with my code?
>
> Thanks to those who can help.
>
> Alice.
>
> ======================================================
> Alice Wei
> MIS 2008
> School of Library and Information Science
> Indiana University Bloomington
> ***@indiana.edu
>
> _______________________________________________
> ***@x-query.com
> http://x-query.com/mailman/listinfo/talk


--
John Snelson, Oracle Corporation
Berkeley DB XML: http://www.oracle.com/database/berkeley-db/xml
XQilla: http://xqilla.sourceforge.net
Continue reading on narkive:
Loading...