首页 > 解决方案 > Saxon 9 HE、Java - 静态错误、XTSE0210、XTSE0165、XPST0017

问题描述

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

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 时,它会成功运行。

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

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

  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);
    }
  }

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

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

知道可能需要对代码进行哪些调整以帮助其工作吗?

奇怪的是,通过 Saxon 9.4he 运行相同的代码,工作正常。

标签: javaxsltsaxon

解决方案


当你这样做

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

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

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


推荐阅读