Saxon 9 HE,Java - 静态错误,XTSE0210,XTSE0165,XPST0017
Saxon 9 HE, Java - Static errors, 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 时,它有一系列的导入,太大而无法在这里重现。
知道代码中可能需要调整什么以帮助它工作吗?
奇怪的是 运行 相同的代码在 Saxon 9.4he 中有效。
当你这样做时
Templates template = factory.newTemplates(new StreamSource(new StringReader(xslDoc)));
您没有为样式表提供系统 ID(基本 URI)。相比之下,当您从命令行 运行 时,Saxon 可以从提供的文件名中计算出基本 URI。我不知道它为什么会失败,因为你没有提供足够的信息,但我怀疑这是造成差异的根本原因。因为 Saxon 没有关于样式表模块的基本 URI 的完整信息,所以它可能认为两个模块是相同的,而实际上它们并不相同。
您可以提供系统 ID 作为 new StreamSource()
的第二个参数。
从我的应用程序调用使用 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 时,它有一系列的导入,太大而无法在这里重现。
知道代码中可能需要调整什么以帮助它工作吗?
奇怪的是 运行 相同的代码在 Saxon 9.4he 中有效。
当你这样做时
Templates template = factory.newTemplates(new StreamSource(new StringReader(xslDoc)));
您没有为样式表提供系统 ID(基本 URI)。相比之下,当您从命令行 运行 时,Saxon 可以从提供的文件名中计算出基本 URI。我不知道它为什么会失败,因为你没有提供足够的信息,但我怀疑这是造成差异的根本原因。因为 Saxon 没有关于样式表模块的基本 URI 的完整信息,所以它可能认为两个模块是相同的,而实际上它们并不相同。
您可以提供系统 ID 作为 new StreamSource()
的第二个参数。