Saxon 9 HE、Java - 静态错误、XTSE0210、XTSE0165、XPST0017 [英] Saxon 9 HE, Java - Static errors, XTSE0210, XTSE0165, XPST0017

查看:53
本文介绍了Saxon 9 HE、Java - 静态错误、XTSE0210、XTSE0165、XPST0017的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中使用 Saxon 调用 XSL 转换时,我收到以下错误

When invoking an XSL transform using Saxon from my application I receive the following error

Static error at xsl:import on line 34 column 45 
  XTSE0210: A stylesheet cannot import itself
Static error at xsl:import on line 42 column 39 
  XTSE0165: Reported 1 error in imported stylesheet module
Static error in {leg:IsCurrentWelsh(/)} in expression in xsl:when/@test on line 101 column 43 
  XPST0017: Cannot find a 1-argument function named

.
.
.
net.sf.saxon.trans.XPathException: Errors were reported during stylesheet compilation
    at net.sf.saxon.style.StylesheetModule.loadStylesheet(StylesheetModule.java:260) ~[Saxon-HE-9.8.0-15.jar:na]
    at net.sf.saxon.style.Compilation.compileSingletonPackage(Compilation.java:106) ~[Saxon-HE-9.8.0-15.jar:na]
    at net.sf.saxon.s9api.XsltCompiler.compile(XsltCompiler.java:739) ~[Saxon-HE-9.8.0-15.jar:na]
    at net.sf.saxon.jaxp.SaxonTransformerFactory.newTemplates(SaxonTransformerFactory.java:155) ~[Saxon

但是,当从命令行调用 Saxon 时,它可以成功运行.

However when invoking Saxon from the command line it works successfully.

java -jar saxon9he.jar xml.xml {path-to-my-xslt}

在我的 Java 应用程序中调用 XSLT 的代码如下...

The code invoking the XSLT in my Java application is as follows...

  public static String transform(Document inputDoc, String xslDoc, Map<String, Object> params, String xslContextPath) throws XmlException {

    try {
      System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");
      TransformerFactory factory = TransformerFactory.newInstance();
      factory.setURIResolver(new ClasspathResourceURIResolver(xslContextPath));
      factory.setAttribute(FeatureKeys.GENERATE_BYTE_CODE, false);

      Templates template = factory.newTemplates(new StreamSource(new StringReader(xslDoc)));

      Transformer xformer = template.newTransformer();

      if (params != null) {
        for (Map.Entry<String, Object> entry : params.entrySet()) {
          xformer.setParameter(entry.getKey(), entry.getValue());
        }
      }

      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
      DOMSource domSource = new DOMSource(inputDoc);


      xformer.transform(domSource, new StreamResult(outputStream));

      return outputStream.toString("UTF-8");

    } catch (TransformerConfigurationException e) {
      throw new XmlException(e);

    } catch (TransformerException e) {
      SourceLocator locator = e.getLocator();
      if (locator != null) {
        Map<String, Object> message = new HashMap<String, Object>();
        message.put("col", locator.getColumnNumber());
        message.put("line", locator.getLineNumber());
        message.put("publicId", locator.getPublicId());
        message.put("systemId", locator.getSystemId());
        throw new XmlException(message.toString(), e);
      }
      throw new XmlException(e);

    } catch (Exception e) {
      throw new XmlException(e);
    }
  }

对于这两种情况都没有传递其他参数.

no other params are being passed in for either case.

这是使用 Saxon 9.8.0-15he.在大多数情况下,上面的代码工作正常,我们已经使用它很长时间没有问题,这是当我调用一个特定的 XSLT 时,它有一系列导入,太大而无法在此处重现.

This is using Saxon 9.8.0-15he. In most cases the above code is working fine and we have been using it for a long time without issue, it is when I am invoking a particular XSLT, which has a series of imports, too large to reproduce here.

知道需要对代码进行哪些调整才能使其正常工作吗?

Any idea what may need tweaking in the code to help it work?

奇怪地通过 Saxon 9.4he 运行相同的代码,有效.

Strangely running the same code through Saxon 9.4he, works.

推荐答案

什么时候做

Templates template = factory.newTemplates(new StreamSource(new StringReader(xslDoc)));

您没有为样式表提供系统 ID(基本 URI).相比之下,当您从命令行运行时,Saxon 可以根据提供的文件名计算出基本 URI.我不知道它为什么会失败,因为您没有提供足够的信息,但我怀疑这是造成差异的根本原因.因为 Saxon 没有关于样式表模块的基本 URI 的不完整信息,所以它可能认为两个模块是相同的,但实际上它们不同.

you're not providing a system ID (base URI) for the stylesheet. By contrast, when you run from the command line, Saxon can work out a base URI from the supplied filename. I don't know exactly why it's failing, because you haven't provided enough information, but I suspect this is the basic cause of the difference. Because Saxon has incomplete information about base URIs of stylesheet modules, it probably thinks two modules are the same when they aren't.

您可以提供系统 ID 作为 new StreamSource() 的第二个参数.

You can supply a system ID as the second argument of new StreamSource().

这篇关于Saxon 9 HE、Java - 静态错误、XTSE0210、XTSE0165、XPST0017的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆