XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
Neal WaltersSubject: Counting distinct node types
Author: Neal Walters
Date: 24 Jul 2006 03:00 PM
Originally Posted: 24 Jul 2006 02:59 PM
Is there an equivalent XQuery for this SQL:

select count(actionCode) distinct from data

So for example, I might have a big XML file with lots of books, and the books can be fiction, cooking, and computers.

Can I get a quick count that there are 5 fiction books, 12 cooking books, and 9 computer books?

Thanks in advance,
Neal

Postnext
Ivan PedruzziSubject: Counting distinct node types
Author: Ivan Pedruzzi
Date: 24 Jul 2006 03:35 PM
HI Neal,

Create a new xquery paste the following code and save the query in
c:\Program Files\Stylus Studio 2006 Release 3 XML Enterprise Edition\examples\simpleMappings



declare variable $d := doc("books.xml");

for $subject in distinct-values($d/books/book/subject)
return concat($subject, ' ',count($d/books/book[subject=$subject]))



Hope this helps
Ivan Pedruzzi
Stylus Studio Team

Postnext
Neal WaltersSubject: Counting distinct node types
Author: Neal Walters
Date: 25 Jul 2006 12:24 PM
Thanks Ivan, here's what I did, and it worked:

declare variable $d := doc("c:\CPRBatch_Mapped_July24.xml");
for $type in distinct-values($d//*[local-name()='PaymentRelationship']/@ConstituentActivityType)
return concat($type, '=', count($d//*[local-name()='PaymentRelationship'][@ConstituentActivityType=$type]))

Now, is it possible to use the namespace prefixes?

When I run the one below, it says:
No namespace prefix for 'p5' [err:XPST0081]


declare variable $d := doc("c:\CPRBatch_Mapped_July24.xml");
for $type in distinct-values($d//p5:PaymentRelationship/@ConstituentActivityType)
return concat($type, count($d//p5:PaymentRelationship[@ConstituentActivityType=$type]))

Thanks a lot!
Neal


Postnext
Ivan PedruzziSubject: Counting distinct node types
Author: Ivan Pedruzzi
Date: 25 Jul 2006 12:50 PM

Sure,

declare namespace p5 = "namespace URI here";



Ivan Pedruzzi
Stylus Studio Team

Postnext
Neal WaltersSubject: Counting distinct node types
Author: Neal Walters
Date: 25 Jul 2006 05:07 PM
Cool, that worked. But why is it necessary? All the namespaces are defined in the xml itself. Seems like I would only need this if I wanted a short cut prefix that was different than the ones in the data.

Postnext
Ivan PedruzziSubject: Counting distinct node types
Author: Ivan Pedruzzi
Date: 25 Jul 2006 05:59 PM

Nope, there is no such a thing of inherit the prefix definitions from the input document.

In XSLT and XQuery as well, you have to declare all namespaces you want to use in XPath expressions to use the shorthand version.

Ivan Pedruzzi
Stylus Studio Team

Posttop
Neal WaltersSubject: Counting distinct node types
Author: Neal Walters
Date: 26 Jul 2006 02:49 PM
I realize you didn't write the XSLT/XQuery standards - but it isn't that just crazy? Did they have some logical reason for making that decision - which makes for bulkier code, a lot of copy/paste, especially since the the syntax in XQuery for defining the prefixes is slightly different from that of XML.

This would be a great opportunity for you to provide a neat "import" feature. Click some button, and it would import all namespaces from XML input into XQuery declare statements.

Neal

 
Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.