[XQuery Talk Mailing List Archive Home] [By Date] [By Thread] [By Subject] [By Author] [Recent Entries] [Reply To This Message]

Questions on User-Defined Functions

Michael Kay mike at saxonica.com
Sun Feb 24 19:01:28 PST 2008


  Questions on User-Defined Functions
You can certainly have the function wrap the count in an element if that's
what you want:

declare function local:count-distinct-values($seq as xs:anyAtomicType*)
      as element(statistics) {
   <statistics>{count(distinct-values($seq))}</statistics>
};

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

> -----Original Message-----
> From: Wei, Alice J. [mailto:http://x-query.com/mailman/listinfo/talk] 
> Sent: 24 February 2008 14:04
> To: Michael Kay
> Cc: http://x-query.com/mailman/listinfo/talk
> Subject: RE:  Questions on User-Defined Functions
> 
> Dear Michael:
> 
>    The good thing is that the errors I have had before were 
> gone, but I wanted to have it be output as
> 
>    <statistics>229</statistics>
> 
>    and not only 229.
> 
>    Apparently, it appears that since the function is now 
> defined as xs:integer as the output, it cannot output texts 
> of other format. Therefore, what would you suggest for me to 
> add the tags in?
> 
>   This is what I have come up with:
> 
> declare function local:count-distinct-values($seq as 
> xs:anyAtomicType*)
>       as xs:integer {
>    count(distinct-values($seq))
> };
> <statistics>
> {local:count-distinct-values(collection("xmldb:exist://db/cbml")//ad)}
> </statistics>
> 
> Is this the way to go?
> 
> I know that this script may not be making much sense in terms 
> of its information extraction, but this little code is 
> probably simple enough for me to start off with to 
> investigate further into the subject.
> 
> Alice
> ==============================================
> Alice Wei
> MIS 2008
> School of Library and Information Science Indiana University 
> Bloomington http://x-query.com/mailman/listinfo/talk ________________________________________
> From: Michael Kay [http://x-query.com/mailman/listinfo/talk]
> Sent: Sunday, February 24, 2008 8:21 AM
> To: Wei, Alice J.
> Subject: RE:  Questions on User-Defined Functions
> 
> >
> > I modified my code to what you suggested,
> >
> > declare function local:total($po as element(ad)*)
> >        as xs:integer {
> >     count(distinct-values($po))
> > };
> 
> If you want a function that counts the number of distinct 
> values in a sequence, then it's best to make it work on any 
> sequence, to maximize the scope for reuse. I would define it as:
> 
> declare function local:count-distinct-values($seq as 
> xs:anyAtomicType*)
>        as xs:integer {
>     count(distinct-values($seq))
> };
> 
> There is no need to restrict the caller to supplying elements 
> with a particular name. Declaring the argument as 
> xs:anyAtomicType will automatically atomize the supplied 
> sequence if it contains elements or other nodes. I've also 
> chosen what seem to me more meaningful names for the function 
> and its parameter.
> 
> > However, the
> > number is now in superscript, and I wanted to add the <statistics> 
> > tags back in.
> 
> It's not the function that puts it in superscript - what you 
> do with the result of the function depends entirely on the 
> calling code.
> >
> > I intend to create a web application out of XQuery, and I 
> am not sure 
> > if user-defined functions is the way to go.
> 
> In any programming language, modularizing your code is 
> essential as the application gets bigger, and user-defined 
> functions are the main way to achieve that in XQuery. 
> However, having a function called
> count-distinct-values($x) which does exactly the same as
> count(distinct-values($x)) isn't exactly a great leap of abstraction.
> 
> Michael Kay
> http://www.saxonica.com/
> 



Purchase Stylus Studio Online Today!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2007 All Rights Reserved.