[xquery-talk] Questions on User-Defined Functions

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


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:ajwei at indiana.edu] 
> Sent: 24 February 2008 14:04
> To: Michael Kay
> Cc: talk at x-query.com
> Subject: RE: [xquery-talk] 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 ajwei at indiana.edu ________________________________________
> From: Michael Kay [mike at saxonica.com]
> Sent: Sunday, February 24, 2008 8:21 AM
> To: Wei, Alice J.
> Subject: RE: [xquery-talk] 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/
> 



More information about the talk mailing list