This section describes how XPath values supplied in a call to an extension function are converted to Java values.
The following conversions are supported between the supplied value of the argument and the declared Java class of the argument. The mappings are given in order of preference; a class that appears earlier in the list has smaller "conversion distance" than one appearing later. These priorities are used to decide which method to call when the class has several methods of the same name. Simple classes (such as boolean) are acceptable wherever the corresponding wrapper class (Boolean) is allowed. Class names shown in italics are Saxon-specific classes.
If the supplied value is a singleton (a sequence of one item) then the type of that item is decisive. If it is a sequence of length zero or more than one, then the general rules for a sequence are applied, and the types of the items within the sequence are irrelevant.
Supplied value |
Required type |
boolean |
BooleanValue, Boolean, (String, CharSequence), Byte, Character, Double, Float, Integer, Long, Short, Byte), List, Object |
dateTime |
DateTimeValue, Date, String, Object |
date |
DateValue, Date, (String, CharSequence), Object |
decimal |
DecimalValue, BigDecimal, Double, Float, Long, Integer, Short, Character, Byte, Boolean, (String, CharSequence), List, Object |
double |
DoubleValue, Double, Float, Long, Integer, Short, Character, Byte, Boolean, (String, CharSequence), List, Object |
duration |
DurationValue, (String, CharSequence), Object |
float |
FloatValue, Float, Double, Long, Integer, Short, Character, Byte, Boolean, (String, CharSequence), List, Object |
integer |
IntegerValue, Long, Double, Float, Integer, Short, Character, Byte, Boolean, (String, CharSequence), List, Object |
string |
StringValue, (String, CharSequence), Character, Double, Float, Integer, Long, Short, Boolean, Byte, List, Object |
anyURI |
AnyURIValue, java.net.URI, java.net.URL, (String, CharSequence), List, Object |
node |
SingletonNodeSet, NodeList, (Element, Attr, Document, DocumentFragment, Comment, Text, ProcessingInstruction, CharacterData), Node, Boolean, Byte, Character, Double, Float, Integer, Long, Short, (String, CharSequence), Object |
sequence |
SequenceIterator, SequenceValue, List, NodeList, NodeInfo, Node, (String, CharSequence), Boolean, Byte, Character, Double, Float, Integer, Long, Short, Object |
Saxon first tries to select the appropriate method based on the static type of the arguments to the function call. If there is insufficient information statically, it tries again at run-time, based on the dynamic type of the arguments once evaluated. This means that the same function call may invoke different methods on different occasions.
Note that the XPath value is considered to be one of the singleton classes if it is produced by an
expression that always produces a singleton. So the expression 1+2
will be an integer. An
expression that can potentially produce a sequence of any cardinality is represented in the table above
by the generic class "sequence". For example, ($a to $b)
is represented as a sequence, except
when it can be determined statically that $a
and $b
are equal, in which case it is represented as an
integer.
These rules will probably be rationalized further in future releases.
A wrapped Java object may be converted to another data type as follows.
It is converted to a string by using its toString() method; if the object is null, the result is the empty string "".
It is converted to a number by converting it first to a string, and then applying the XPath number() conversion. If it is null, the result is NaN.
It is converted to a boolean as follows: if it is null, the result is false, otherwise it is converted to a string and the result is true if and only if the string is non-empty.