On 29 Jul 2023, at 13:48, Paul Hoadley <[email protected]> wrote:
> I'll put together a minimal example and see if you can put me in the right
> direction.
To recap, I'm building a Java project that will encapsulate the DocBook
stylesheets and some classes to do some transformations, all packaged as a JAR
to become part of a larger web app. The project uses the standard Maven layout,
and Saxon-HE 12.3. I've put the XSL in src/main/resources/xsl:
paulh@elmo xsl % pwd
/Users/paulh/Projects/Java/janus/src/main/resources/xsl
paulh@elmo xsl % ls -l
total 688
drwxr-xr-x@ 48 paulh staff 1536 29 Jul 2020 docbook-xsl-1.79.2
-rw-r--r-- 1 paulh staff 16681 24 Jul 10:41 header-footer.xsl
-rw-r--r-- 1 paulh staff 7494 28 Jul 14:23 juno-driver.xsl
-rw-r--r-- 1 paulh staff 8689 24 Jul 14:33 table.xsl
-rw-r--r-- 1 paulh staff 307717 18 Jul 12:49 titlepage.xsl
juno-driver.xsl is the top-level customisation stylesheet, and it imports the
others:
<xsl:import href="docbook-xsl-1.79.2/fo/docbook.xsl" />
<xsl:import href="titlepage.xsl" />
<xsl:import href="table.xsl" />
<xsl:import href="header-footer.xsl" />
To be clear, addressing the XSL stylesheets as files on the filesystem works
just fine. That is, this does exactly what it should:
private Document transformDocument(Document document) throws
TransformerException, FileNotFoundException {
DOMResult result = new DOMResult();
TransformerFactory factory = TransformerFactory.newInstance();
factory.setURIResolver(new StandardURIResolver());
InputStream is = new FileInputStream(new
File("/Users/paulh/Projects/Java/janus/src/main/resources/xsl/juno-driver.xsl"));
Source source = new StreamSource(is,
"file:/Users/paulh/Projects/Java/janus/src/main/resources/xsl/juno-driver.xsl");
Transformer transformer = factory.newTransformer(source);
transformer.transform(new DOMSource(document), result);
return (Document) result.getNode();
}
What I want to do, though, is reference the stylesheets as classpath resources.
StandardURIResolver claims to be able to handle the "classpath URI scheme", so
I tried this:
private Document transformDocument(Document document) throws
TransformerException, FileNotFoundException {
DOMResult result = new DOMResult();
TransformerFactory factory = TransformerFactory.newInstance();
factory.setURIResolver(new StandardURIResolver());
InputStream is =
XmlTest.class.getResourceAsStream("/xsl/juno-driver.xsl");
Source source = new StreamSource(is, "classpath:/xsl/juno-driver.xsl");
Transformer transformer = factory.newTransformer(source);
transformer.transform(new DOMSource(document), result);
return (Document) result.getNode();
}
Which results in:
Error
XTSE0165: I/O error reported by XML parser processing
classpath:xsl/docbook-xsl-1.79.2/fo/docbook.xsl: unknown protocol: classpath
javax.xml.transform.TransformerConfigurationException:
net.sf.saxon.s9api.SaxonApiException: I/O error reported by XML parser
processing classpath:xsl/docbook-xsl-1.79.2/fo/docbook.xsl
at
net.sf.saxon.jaxp.SaxonTransformerFactory.newTemplates(SaxonTransformerFactory.java:158)
at
net.sf.saxon.jaxp.SaxonTransformerFactory.newTransformer(SaxonTransformerFactory.java:112)
at net.logicsquad.janus.XmlTest.transformDocument(XmlTest.java:142)
If StandardURIResolver can handle classpath URIs, have I just got the syntax or
other usage wrong somewhere?
--
Paul Hoadley
https://logicsquad.net/
https://www.linkedin.com/company/logic-squad/