问题
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
However when invoking Saxon from the command line it works successfully.
java -jar saxon9he.jar xml.xml {path-to-my-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.
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?
Strangely running the same code through Saxon 9.4he, works.
回答1:
When you do
Templates template = factory.newTemplates(new StreamSource(new StringReader(xslDoc)));
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.
You can supply a system ID as the second argument of new StreamSource()
.
来源:https://stackoverflow.com/questions/60670920/saxon-9-he-java-static-errors-xtse0210-xtse0165-xpst0017