2. Java™ methods as extension functions
Prev | Chapter 7. XPath functions |

## 2. Java™ methods as extension functions

### Note

The following section has been adapted from the documentation of James Clark's XT, one of the fastest XSLT engines, from which the XXE implementation of XPath has been extracted.

A call to a function `ns:foo` where `ns` is bound to a namespace of the form `java:`className`` is treated as a call of the static method `foo` of the class with fully-qualified name `className`. Example:

    xmlns:file="java:java.io.File"

    file:createTempFile('xxe', '.tmp')

Hyphens in method names are removed with the character following the hyphen being upper-cased. Example:

    file:create-temp-file('xxe', '.tmp')

is equivalent to:

    file:createTempFile('xxe', '.tmp')

A non-static method is treated like a static method with the this object as an additional first argument. Example:

    file:delete-on-exit(file:createTempFile('xxe', '.tmp'))

A constructor is treated like a static method named `new`. Example:

    xmlns:url="java:java.net.URL"

    url:new('http://www.xmlmind.com/xmleditor/')

Overloading based on number of parameters is supported; overloading based on parameter types is not. Example, it is possible to invoke:

    url:new('http://www.xmlmind.com/xmleditor/')

though both `java.net.URL(java.lang.String spec)` and `java.net.URL(java.net.URL context, java.lang.String spec)` exist. It is not possible to invoke:

    file:new('.')

because both `java.io.File(java.lang.String pathname)` and `java.io.File(java.net.URI uri)` exist.

Extension functions can return objects of arbitrary types which can then be passed as arguments to other extension functions.

Types are mapped between XPath and Java™ as follows:

XPath type | Java™ type
--- | ---
string | `java.lang.String`
number | double
boolean | boolean
node-set | `com.xmlmind.xml.xpath.NodeIterator`

On return from an extension function, an object of type `com.xmlmind.xml.doc.XNode` is also allowed and will be treated as a node-set; also any numeric type is allowed and will be converted to a number.

Prev | Up |
1. Extension functions | Home |