开发者社区 问答 正文

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

两种情况都不会传递其他参数。

这是使用Saxon 9.8.0-15he。在大多数情况下,以上代码运行良好,并且我们已经使用了很长时间,没有问题,这是在我调用特定的XSLT的时候,该XSLT具有一系列导入,太大了,无法在此处重现。

任何想法可能需要调整代码以帮助其工作吗?

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

问题来源:Stack Overflow

展开
收起
montos 2020-03-27 10:44:00 543 分享 版权
1 条回答
写回答
取消 提交回答
  • 当你做

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

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

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

    回答来源:Stack Overflow

    2020-03-27 10:44:22
    赞同 展开评论
问答分类:
问答标签:
问答地址: