Hi Kristian,
Why can't I specify xs:positiveIntegers as literals? Or why can't I treat xs:integers as xs:positiveIntegers?
The error is correct. Type handling in XQuery is a topic on its own: Values can be cast, promoted, treated as, etc. in this language. If you want to dive into the details, I suggest having a look at the section on type promotion in the specification:
http://www.w3.org/TR/xquery-31/#id-type-promotion-and-operator-mapping
If you are looking for a practical solution, I advise you to simply use with xs:integer, even if a value will always be positive, or if it will have a smaller bit range. Most operations have been optimized for integers, so you won't lose any time for additional type conversion.
Hope this helps, Christian
Consider a simple function that takes a
xs:positiveInteger as input:
<code> xquery version "3.0" encoding "UTF-8"; declare namespace local = "local";
declare function local:positive( $input as xs:positiveInteger ) as xs:positiveInteger { $input };
</code>
I get an error XPTY0004 Cannot treat xs:integer as xs:positiveInteger if I run it with a literal input, like this:
<code> local:positive(2) </code>
I get no error if I specify the type explicitly, like this:
<code> local:positive(xs:positiveInteger(2)) </code>
This feel very odd, since the type system is hierarchical and xs:positiveInteger is a subcase of xs:integer, I don't really see a problem of this kind of automatic casting of integer literals if they qualify the cast.
Can you give any reflection on this?
Best regards Kristian Kankainen