微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Saxonica URIResolver 中的异常处理

如何解决Saxonica URIResolver 中的异常处理

我正在使用 saxonica EE 版本进行 xslt 转换,并从自定义 URI 解析器类(如下所示)抛出异常,它对 #include 工作正常,但同样不适用于 #document(),

  1. 无论如何我们可以通过在解析 document() 时抛出异常来停止转换。
  2. 是否可以在编译期间(在生成 SEF 时)将 URI 解析器应用于 document()。

公共类 CustomURIResolver 实现 URIResolver {

    @Override
    public Source resolve(String href,String base) {
                String formatterOrlookUpKey = getKey(href);
        if (formatterMap.containsKey(formatterOrlookUpKey)) {
            return new StreamSource(new StringReader(formatterMap.get(formatterOrlookUpKey)));
        } else {
            throw new RuntimeException("did not find the lookup/formatter xsl " + href+" key:"+formatterOrlookUpKey);
        }

    }}

XSLT 编译:

Processor processor = new Processor(true);
            XsltCompiler compiler = processor.newXsltCompiler();
            compiler.setJustInTimeCompilation(false);
            compiler.setURIResolver(new CigURIResolver(formatterMap));
            XsltExecutable stylesheet = compiler.compile(new StreamSource(new StringReader(xsl)));
            stylesheet.export(destination);

转型

Processor processor = new Processor(true);
        XsltCompiler compiler = processor.newXsltCompiler();
        compiler.setJustInTimeCompilation(true);
            XsltExecutable stylesheet = compiler.compile(new StreamSource(new StringReader(sef)));

        final StringWriter writer = new StringWriter();
        Serializer out = processor.newSerializer(writer);
        out.setoutputProperty(Serializer.Property.METHOD,"xml");
        out.setoutputProperty(Serializer.Property.INDENT,"yes");
        Xslt30Transformer trans = stylesheet.load30();
        trans.setURIResolver(new CigURIResolver(formatterMap));
        trans.setErrorListener(errorHandler);
        trans.transform(new StreamSource(new StringReader(xml)),out);
        Object obj = out.getoutputDestination();

解决方法

我对观察到的效果感到有些惊讶,需要一个重现来调查它。但我也有点惊讶您选择抛出 RuntimeException,而不是 URIResolver 接口声明的 TransformerException。如果您想进一步探索这一点,请使用可运行的代码提出支持请求。

由于 XSLT 1.0 遗留的“可恢复错误”,document() 的规则有点复杂:您可能会发现 doc() 的行为更可预测。

关于 doc() 调用的编译时解析,Saxon 确实有一个选项可以启用它,但它不能很好地处理 SEF 文件:通常在 SEF 文件中包含外部文档会变得非常混乱,特别是如果例如,您将多个全局变量绑定到同一文档的不同部分。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。