Using Saxon Integrated Extension Functions
Saxon, the transformation and validation engine used by Oxygen XML Editor, can be customized by adding custom functions (called Integrated Extension Functions) that can be called from XPath.
To define such a function, follow these steps:
- Create a file with a Java class that extends
net.sf.saxon.lib.ExtensionFunctionDefinition
. Here is an example:private static class ShiftLeft extends ExtensionFunctionDefinition { @Override public StructuredQName getFunctionQName() { return new StructuredQName("eg", "http://example.com/saxon-extension", "shift-left"); } @Override public SequenceType[] getArgumentTypes() { return new SequenceType[] {SequenceType.SINGLE_INTEGER, SequenceType.SINGLE_INTEGER}; } @Override public SequenceType getResultType(SequenceType[] suppliedArgumentTypes) { return SequenceType.SINGLE_INTEGER; } @Override public ExtensionFunctionCall makeCallExpression() { return new ExtensionFunctionCall() { public SequenceIterator call(SequenceIterator[] arguments, XPathContext context) throws XPathException { long v0 = ((IntegerValue)arguments[0].next()).longValue(); long v1 = ((IntegerValue)arguments[1].next()).longValue(); long result = v0<<v1; return Value.asIterator(Int64Value.makeIntegerValue(result)); } }; } }
- Compile the class and add it to a JAR file.
- Add a file called net.sf.saxon.lib.ExtensionFunctionDefinition that contains the
fully qualified name of the Java class in the META-INF/services/
folder of the JAR file.Note: To add more function definitions in the same JAR file, you need to add their fully qualified names on different lines.
To enable Oxygen XML Editor to pick up your custom function definition, the JAR file
should be added to the classpath of the transformer. Here are some possibilities:
- If you develop a framework, you just need to link the JAR file in the Classpath tab.
- In a validation scenario, you can use the Extensions button to open a dialog box where you can add libraries.
- In a transformation scenario, you can use the Extensions button in the XSLT tab to open a dialog box where you can add libraries.
- You can also create a plugin that contributes such a JAR file in the classpath.