准备时使用源解析器
通过在准备可执行文件时指定源解析器,可以告知处理器如何解释此时引用的 URI。
过程
在准备可执行文件时指定源解析器。
这会告知处理器如何解释此时引用的 URI,例如,在样式表的 xsl:import 和 xsl:include 伪指令中。
缺省源解析行为是在基本 URI 可用的情况下,根据表达式、查询或样式表的基本 URI 解释相对 URI,或在基本 URI 不可用的情况下,将它们解释为相对于当前工作目录的文件路径。使用绝对 URI 保持不变。
要更改此行为,请先编写 Java™ 类,此类用于实现 XSourceResolver 接口并向 XStaticContext 注册此接口,然后准备样式表。
以下是如何注册源解析器的基本示例。
XFactory factory = XFactory.newInstance();
// Register the source resolver with the static context
XStaticContext staticContext = factory.newStaticContext();
XSourceResolver sourceResolver=new ASourceResolver(replacementBase);
staticContext.setSourceResolver(sourceResolver);
// Prepare the sylesheet
XSLTExecutable executable = factory.prepareXSLT(new StreamSource(stylesheetFile), staticContext);
XDynamicContext dynamicContext = factory.newDynamicContext();
// Execute the stylesheet
XSequenceCursor cursor = executable.execute(new StreamSource(inputFile), dynamicContext);