This project has retired. For details please refer to its Attic page.
Apache ODE – Custom XPath Functions

Custom XPath Functions

Introduction

ODE uses the Saxon XPath processor to support custom XPath functions.

This text is a quick summary of Saxon's own extensibility documentation. I recommend you read it if you need more information. It goes into much more detail on Java invocation, supported data types, conversions, etc.

To use XPath extensions, you must use the XPath 2.0 expression language.

Writing extension functions

An extension function is invoked using a name such as prefix:localname(). The prefix must be the prefix associated with a namespace declaration that is in scope.

Extension functions must be implemented in Java. You bind external Java classes by encoding the class name part in the namespace URI. The URI for the namespace identifies the class where the external function will be found. The namespace URI must be java: followed by the fully-qualified class name; for example xmlns:date="java:java.util.Date"). The class must be on the classpath, or more specifically accessible from the current classloader.

Saxon 8.x and earlier supported namespace binding with a training "/" + the fully-qualified class name; for example xmlns:date="http://www.jclark.com/xt/java/java.util.Date"). This approach is no longer supported in Saxon 9.x and you should now use the java: prefix.
<assign>
    <copy xmlns:ext="java:com.example.xpath.Random">
        <from expressionLanguage="urn:oasis:names:tc:wsbpel:2.0:sublang:xpath2.0">ext:random()</from>
        <to>$myVariable</to>
    </copy>
</assign>

:::java
package com.example.xpath;

/**
 * Returns a random number
 */
public class Random {
    public static int random() {
        java.util.Random randomizer = new java.util.Random();
        int number = randomizer.nextInt();
        return Math.abs(number);
    }
}

You can download source code or the ready-to-use jar for testing.

Deploying extension functions

There are a few options:

  • As a generic solution, you can add your XPath function jar in the application CLASSPATH
  • If ODE is deployed as a webapp, you can copy your XPath function jar in the webapp /lib directory
  • If ODE is deployed inside a JBI container, you can deploy your XPath function jar as a shared library