[xquery-talk] XQuery treatment of xs:positiveInteger?

Jeni Tennison jeni at jenitennison.com
Tue Jul 20 15:44:12 PDT 2004

Till wrote:
> So the constants '2' and '3' are of type xs:integer and that's a
> supertype of xs:positiveInteger and as such cannot be substituted.

The lesson here is that to avoid users having to do casts themselves
you should define functions so that they're generous in what they
accept. This is particularly true when it comes to functions that
accept numbers or strings because it's so much easier for someone
using your function to write the literals than the casts. Consider:

  my:simple-add(2, 3)

compared to:

  my:simple-add(xs:positiveInteger(2), xs:positiveInteger(3))

You can do the casts yourself, in the body of the function, and thus
you still get the benefits of the type checking. In this example,
something like:

  declare function my:simple-add($xi as xs:integer,
                                 $yi as xs:integer)
                                as xs:positiveInteger {
    let    $x := xs:positiveInteger($xi),
           $y := xs:positiveInteger($yi)
    return $x + $y

You'll notice that several of the built-in functions are designed in
this way: the actual restrictions on the arguments are described in
the definition of the function rather than via the declared types of
the arguments. For example, see fn:expanded-QName [1].



[1] http://www.w3.org/TR/xpath-functions/#func-expanded-QName

Jeni Tennison

More information about the talk mailing list