Thanks again Martin, your really helped me out! I wrote my own custom xslt function with the integrated extension functions feature. The function calls a java method, which tests if a file is present in a given directory and returns either true or false. For those who need an working example of the "integrated extension functions" feature, or even want to test if a file exists with the saxon-9-HE, I will share my simple solution.
Java class which defines the xslt function name, arguments and return type and harbours the java method to call, when the xslt function is invoked:
package de.mypackage.xsltfunctions;
import java.io.File;
import net.sf.saxon.expr.XPathContext;
import net.sf.saxon.lib.ExtensionFunctionCall;
import net.sf.saxon.lib.ExtensionFunctionDefinition;
import net.sf.saxon.om.Sequence;
import net.sf.saxon.om.StructuredQName;
import net.sf.saxon.trans.XPathException;
import net.sf.saxon.value.BooleanValue;
import net.sf.saxon.value.SequenceType;
import net.sf.saxon.value.StringValue;
public class FileExists extends ExtensionFunctionDefinition {
@Override
public StructuredQName getFunctionQName() {
return new StructuredQName("file", "http://mydomain.de/xslt/filesystem", "file-exists");
}
@Override
public SequenceType[] getArgumentTypes() {
return new SequenceType[] { SequenceType.SINGLE_STRING, SequenceType.SINGLE_STRING };
}
@Override
public SequenceType getResultType(final SequenceType[] suppliedArgumentTypes) {
return SequenceType.SINGLE_BOOLEAN;
}
@Override
public ExtensionFunctionCall makeCallExpression() {
return new ExtensionFunctionCall() {
@Override
public Sequence call(final XPathContext context, final Sequence[] arguments)
throws XPathException {
String searchDir = ((StringValue) arguments[0]).getStringValue();
String fileName = ((StringValue) arguments[1]).getStringValue();
if (!new File(searchDir).isDirectory()) {
throw new XPathException(
"First argument \"" + searchDir + "\" is not a directory or cannot be found!");
}
return BooleanValue.get(new File(searchDir + fileName).exists());
}
};
}
}
Code snipped which registers the custom xslt-function for the saxon processor:
import java.io.StringWriter;
import de.mypackage.xsltfunctions.FileExists;
import net.sf.saxon.TransformerFactoryImpl;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
StringWriter xmlResultResource = new StringWriter();
System.setProperty("javax.xml.transform.TransformerFactory","net.sf.saxon.TransformerFactoryImpl");
TransformerFactory factory = TransformerFactory.newInstance();
TransformerFactoryImpl tFactoryImpl = (TransformerFactoryImpl) factory;
net.sf.saxon.Configuration saxonConfig = tFactoryImpl.getConfiguration();
saxonConfig.registerExtensionFunction(new FileExists());
Transformer transformer = factory.newTransformer(new StreamSource(getXslFile()));
transformer.transform(new StreamSource(xmlFileInput), new StreamResult(xmlResultResource));
String result = xmlResultResource.getBuffer().toString();