Java Extension Functions
A Java extension function can be used within an XPath or XQuery expression to invoke a Java constructor or call a Java method (static or instance).
A field in a Java class is considered to be a method without any argument. A field can be static or instance. How to access fields is described in the respective sub-sections, static and instance.
This section is organized into the following sub-sections:
•Java: Static Methods and Static Fields
•Java: Instance Methods and Instance Fields
•Datatypes: XPath/XQuery to Java
•Datatypes: Java to XPath/XQuery
Note the following
•If you are using an Altova desktop product, the Altova application attempts to detect the path to the Java virtual machine automatically, by reading (in this order): (i) the Windows registry, and (ii) the JAVA_HOME environment variable. You can also add a custom path in the Options dialog of the application; this entry will take priority over any other Java VM path detected automatically.
•If you are running an Altova server product on a Windows machine, the path to the Java virtual machine will be read first from the Windows registry; if this is not successful the JAVA_HOME environment variable will be used.
•If you are running an Altova server product on a Linux or macOS machine, then make sure that the JAVA_HOME environment variable is properly set and that the Java Virtual Machines library (on Windows, the jvm.dll file) can be located in either the \bin\server or \bin\client directory.
Form of the extension function
The extension function in the XPath/XQuery expression must have the form prefix:fname().
•The prefix: part identifies the extension function as a Java function. It does so by associating the extension function with an in-scope namespace declaration, the URI of which must begin with java: (see below for examples). The namespace declaration should identify a Java class, for example: xmlns:myns="java:java.lang.Math". However, it could also simply be: xmlns:myns="java" (without a colon), with the identification of the Java class being left to the fname() part of the extension function.
•The fname() part identifies the Java method being called, and supplies the arguments for the method (see below for examples). However, if the namespace URI identified by the prefix: part does not identify a Java class (see preceding point), then the Java class should be identified in the fname() part, before the class and separated from the class by a period (see the second XSLT example below).
Note: | The class being called must be on the classpath of the machine. |
XSLT example
Here are two examples of how a static method can be called. In the first example, the class name (java.lang.Math) is included in the namespace URI and, therefore, must not be in the fname() part. In the second example, the prefix: part supplies the prefix java: while the fname() part identifies the class as well as the method.
<xsl:value-of xmlns:jMath="java:java.lang.Math"
select="jMath:cos(3.14)" />
<xsl:value-of xmlns:jmath="java"
select="jmath:java.lang.Math.cos(3.14)" />
The method named in the extension function (cos() in the example above) must match the name of a public static method in the named Java class (java.lang.Math in the example above).
XQuery example
Here is an XQuery example similar to the XSLT example above:
<cosine xmlns:jMath="java:java.lang.Math">
{jMath:cos(3.14)}
</cosine>
User-defined Java classes
If you have created your own Java classes, methods in these classes are called differently according to: (i) whether the classes are accessed via a JAR file or a class file, and (ii) whether these files (JAR or class) are located in the current directory (the same directory as the XSLT or XQuery document) or not. How to locate these files is described in the sections User-Defined Class Files and User-Defined Jar Files. Note that paths to class files not in the current directory and to all JAR files must be specified.
Note: | If you wish to add a namespace to an XSLT stylesheet being generated from an SPS created in StyleVision, the namespace must be added to the top-level schema element of the XML Schema on which the SPS is based. Note that the following namespace declaration xmlns:java="java" is created automatically by default in every SPS created in StyleVision. |