By specifying a result resolver at execution time, you can tell the processor how to redirect output URIs specified in the executable.
Result resolvers perform essentially the same function as source resolvers, but on the output side of the processor. They allow you to intercept and redirect output URIs specified in the executable, such as xsl:result-document directives in a stylesheet.
The default resolution behavior is to use the base output URI to resolve result documents if the URI reference is relative. Absolute URIs are used unchanged.
class AResultResolver implements XResultResolver
{
String _replacementBase;
public AResultResolver(String replacementBase)
{
_replacementBase=replacementBase;
}
// Resolve URIs by loading the resource as an XSLT stylesheet
// and evaluating it - return the result as the Source to use
public Result getResult(String href, String base) {
String rebasePrefix="rebase://";
if(href.startsWith(rebasePrefix))
{
href=href.substring(rebasePrefix.length());
base=_replacementBase;
}
java.net.URI baseURI;
Result result=null;
try {
// Get base URI object
baseURI = new java.net.URI(base);
// Resolved relative reference against base URI
URI resolvedURI = baseURI.resolve(href);
// Try to read...
result = new StreamResult(resolvedURI.toString());
} catch (java.net.URISyntaxException use) {
throw new RuntimeException(use);
}
return result;
}
}
XFactory factory = XFactory.newInstance();
XStaticContext staticContext = factory.newStaticContext();
// Prepare the stylesheet
XSLTExecutable executable = factory.prepareXSLT(new StreamSource(stylesheetFile), staticContext);
XDynamicContext dynamicContext = factory.newDynamicContext();
// Register the result resolver with the dynamic context
XResultResolver resultResolver=new AResultResolver(replacementBase);
dynamicContext.setResultResolver(resultResolver);
// Execute the XPath expression
XSequenceCursor cursor = executable.execute(new StreamSource(inputFile), dynamicContext);