Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
The second argument has a type of
xs:integer
. There is no cardinality specified, which means that the default cardinality is used: the effect of this is that the sequence supplied as the argument value must contain exactly one item. The item type for this argument is
xs:integer
, which means that the supplied value must be an atomic value labeled as an
xs:integer
, or as a subtype of
xs:integer
(for example, it might be labeled as an
xs:positiveInteger
). Supplying any other value would lead to a type error, which might be reported either when the expression is compiled or when it is subsequently evaluated.
Actually, the type system is not quite as rigid as this. Instead of supplying an
xs:integer
for the second argument, you can also supply:
However, you cannot supply a string (even a string that obviously contains an integer, such as
“17”
), and you cannot supply a value of a different numeric type, such as
xs:decimal
or
xs:double
. You can use an
xs:integer
where an
xs:double
is expected, but not the other way around.
When the function call expects an atomic value and the supplied value is a node, the system goes through a process called
atomization
to extract the typed value of the node. Atomization is applied to the supplied value (a sequence) to produce a derived value (the atomized sequence). The rules are:
The atomized sequence is then checked against the type given in the function signature. The cardinality of the sequence as a whole must match the cardinality constraints given in the function signature, and each item in the sequence must match the item type given.
The detailed syntax for describing the allowed type of each function argument is given in Chapter 11, where it is referred to as a sequence type descriptor. The detailed rules for deciding whether a particular value is allowed as an argument to a function call, and the way it is converted to the required type when necessary, are given in Chapter 7, in the section describing function calls on page 544.
Function calls are not the only place where a value needs to be checked against a required type. We've already seen that the same rules apply to XSLT variables and parameters. Many of the XPath operators, such as
+
,
-
, and
|
, also have rules saying what type of operands are acceptable. These rules are based on the rules for function calls, but they are slightly different because XPath allows operators to be polymorphic: that is, the same operator can mean different things depending on the types of the arguments supplied. This is not currently allowed for function calls. For each operator, the rules are therefore slightly different, and they are described in this book in the section dealing with each operator. The non-trivial examples are the
=
family of operators and the arithmetic operators, which are all described in Chapter 8.
XQuery chose not to use the function calling rules for variable assignment, but instead applies a stricter criterion. When you write in XQuery
let $x as xs:integer* := my:function(12)
, the result of
my:function(12)
must actually be a sequence of integers; no conversions such as atomization, or casting of untyped atomic values, are permitted in this context.