SAXONICA |
Static methods can be called directly.
The localname of the function must match the name of a public static method in this class. The names
match if they contain the same characters, excluding hyphens and forcing any character that follows
a hyphen to upper-case. For example the XPath function call To-string()
matches the .NET method
ToString()
; but the function call can also be written as ToString()
if you prefer.
If there are several methods in the class that match the localname, and that have the correct number
of arguments, then the system attempts
to find the one that is the best fit to the types of the supplied arguments: for example if the
call is f(1,2)
then a method with two int
arguments will be preferred
to one with two float
arguments. The rules for deciding between methods are quite complex. Essentially, for each candidate method,
Saxon calculates the "distance" between the types of the supplied arguments and the .NET class of the
corresponding argument in the method's
signature, using a set of tables given below. For example, the distance between the XPath data type xs:integer
and the .NET type long
is very small, while the distance between an XPath xs:integer
and a
.NET bool
is much larger. If there is one candidate method where the distances of all arguments are less-than-or-equal-to
the distances computed for other candidate methods, and the distance of at least one argument is smaller,
then that method is chosen.
If there are several methods with the same name and the correct number of arguments, but none is preferable to the others under these rules, an error is reported: the message indicates that there is more than one method that matches the function call.
This binding is carried out statically, using the static types of the supplied arguments, not the dynamic
types obtained when the arguments are evaluated. If there is insufficient static type information to distinguish
the candidate methods, an error is reported. You can supply additional type information using the treat as
expression, or by casting. Often it is enough simply to declare the types of the variables used as arguments to the function
call.
For example (in XSLT):
<xsl:value-of select="math:sqrt($arg)"
xmlns:math="clitype:System.Math"/>
This will invoke the static method System.Math#sqrt()
, applying it to the value of the variable
$arg
, and copying the value of the square root of $arg
to the result tree.
The value of $arg must be convertible to a double under the same rules as for a native XPath function call.
Similarly (in XQuery):
<a xmlns:double="type:System.Double"/>
{double:MaxValue()} </a>
This will output the value of the static field System.Double#MaxValue
. (In practice, it
is better to declare the namespace in the query prolog, or predeclare it using the API, because it will then not
be copied to the result tree.)