Static methods can be called directly.
For example (in XSLT):
<xsl:value-of select="math:sqrt($arg)"
xmlns:math="java:java.lang.Math"/>
This will invoke the static method java.lang.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.
Similarly (in XQuery):
<a xmlns:double="java:java.lang.Double">
{double:MAX_VALUE()} </a>
This will output the value of the static field java.lang.Double#MAX_VALUE
. (In practice, it
is better to declare the namespace in the query prolog, because it will then not be copied to the result tree.)
A static Java method called as an extension function may have an extra first argument of
class net.sf.saxon.expr.XPathContext
.
This argument is not
supplied by the calling XPath or XQuery code, but by Saxon itself.
The XPathContext
object provides methods to access many
internal Saxon resources, the most useful being getContextItem()
which returns the context item
from the dynamic context. The XPathContext object is available with static or instance-level methods, but
not with constructors.
The following example shows a function that obtains the line number of the context node (this is actually a built-in Saxon extension):
/**
* Return the line number of the context node.
*/
public static int lineNumber(XPathContext c) {
Item item = c.getCurrentIterator().current();
if (item instanceof NodeInfo) {
return ((NodeInfo)item).getLineNumber();
} else {
return -1;
}
}
If this method appears in class com.example.code.NodeData
, then it can be accessed
using the following code in XSLT:
<xsl:value-of select="nd:line-number()"
xmlns:nd="java:com.example.code.NodeData"/>
or the following in XQuery:
<line xmlns:nd="java:com.example.code.NodeData">
{ nd:line-number() }
</line>