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

XQuery - Grammar? Implementations?

Michael Kay mike at saxonica.com
Sat Jan 31 10:05:27 PST 2009

  XQuery - Grammar? Implementations?
> I am sometimes fighting to understand all the details of the 
> XQuery grammar. If you have some time, I would be delighted 
> if you could have a look at the following example: I have 
> defined a function with a single string argument:
>   [0]  declare function local:f($a as xs:string) { $a };
> If I pass on an element as follows:
>   [1]  let $a := <X>Y</X> return local:f($a)
> it is accepted by most XQuery implementations which I tried 
> (I am testing with Saxon, BaseX, MonetDB, Zorba, Qizx, Kawa 
> and MXQuery). 

Yes, the function calling rules say that you atomize the element, which
returns an untypedAtomic value, and in a function call untypedAtomic gets
converted to string.

However, when I cast the element before passing it on:
>   [2]  let $a as xs:string := <X>Y</X> return local:f($a)
> some implementations (also Saxon, which I regard as reference 
> implementation) return [XPTY0004] as error. 

There's no "cast" here, just a type declaration for the variable.
Unfortunately, for reasons that I think are completely misguided, the rules
for assigning to variables are different from the rules for supplying values
to function parameters, and do not allow implicit atomization or conversion
from untypedAtomic to string.

A third version 
> with an explicit cast:
>   [3]  let $a := xs:string(<X>Y</X>) return local:f($a)
> is finally accepted by all implementations.

Yes, in this case the element is implicitly atomized to untypedAtomic, and
then the xs:string() constructor function does an explicit conversion.
> If possible, can someone tell me what the difference are 
> between the versions, especially [1] and [2]? I tried to find 
> the answer in the specification, but I was not successful.

The difference is that XQuery does not use the function conversion rules
when assigning to a variable, it insists that the value assigned to the
variable should be precisely the type that is declared.

(This differs from XSLT, where the function conversion rules are used in
both cases.)

Michael Kay


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.
First Name
Last Name
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-2013 All Rights Reserved.