I tried to follow the answer from here Saxon-HE Integrated Extension Functions | how and where? to execute some java methods and return a String, which is kind of the same thing as the example.
Difference is I am running XQuery instead of XSLT.
Problem is I get an error saying no namespace defined when running the XQuery.
Question is can I use the XSLT trick for XQuery ?
Code for now : In specific method :
TransformerFactory factory = TransformerFactory.newInstance();
TransformerFactoryImpl tFactoryImpl = (TransformerFactoryImpl) factory;
config = tFactoryImpl.getConfiguration();
The method used to set the processor :
public void addFunctionLibrary(JavaSaxonLib tsj) {
Processor processor = (Processor) config.getProcessor();
ExtensionFunction javaCall = tsj;
processor.registerExtensionFunction(javaCall);
}
The class to test the ExtensionFunction :
public class JavaSaxonLib implements ExtensionFunction {
String namespace = "";
String nsURL = "";
public JavaSaxonLib(String namespace, String nsURL) {
this.namespace = namespace;
this.nsURL = nsURL;
}
@Override
public QName getName() {
return new QName(nsURL, namespace);
}
@Override
public SequenceType getResultType() {
return SequenceType.makeSequenceType(ItemType.STRING, OccurrenceIndicator.ONE);
}
@Override
public SequenceType[] getArgumentTypes() {
return new SequenceType[] {};
}
@Override
public XdmValue call(XdmValue[] arguments) throws SaxonApiException {
String result = "Saxon is being extended correctly.";
return new XdmAtomicValue(result);
}
}
In the main method :
JavaSaxonLib jsl = new JavaSaxonLib(namespace, nsURL);
saxMan.addFunctionLibrary(jsl);
Thanks ! :-)