Transformation Failing due to xsl:include

I have a Java maven project which includes XSLT transformations. I load the stylesheet as follows:

TransformerFactory tFactory = TransformerFactory.newInstance();

DocumentBuilderFactory dFactory = DocumentBuilderFactory


DocumentBuilder dBuilder = dFactory.newDocumentBuilder();

ClassLoader cl = this.getClass().getClassLoader(); in = cl.getResourceAsStream("xsl/stylesheet.xsl");

InputSource xslInputSource = new InputSource(in);
Document xslDoc = dBuilder.parse(xslInputSource);

DOMSource xslDomSource = new DOMSource(xslDoc);

Transformer transformer = tFactory.newTransformer(xslDomSource);

The stylesheet.xsl has a number of statements. These appear to be causing problems, when I try to run my unit tests I get the following errors:

C:\Code\workspace\app\dummy.xsl; Line #0; Column #0; Had IO Exception with stylesheet file: footer.xsl
C:\Code\workspace\app\dummy.xsl; Line #0; Column #0; Had IO Exception with stylesheet file: topbar.xsl

The include statements in the XSLT are relative links

xsl:include href="footer.xsl"
xsl:include href="topbar.xsl"

I have tried experimenting and changing these to the following - but I still get the error.

xsl:include href="xsl/footer.xsl"
xsl:include href="xsl/topbar.xsl"

Any ideas? Any help much appreciated.

Asked by: Jared422 | Posted: 21-01-2022

Answer 1

Solved my problem using a URIResolver.

class MyURIResolver implements URIResolver {
public Source resolve(String href, String base) throws TransformerException {
  try {
    ClassLoader cl = this.getClass().getClassLoader(); in = cl.getResourceAsStream("xsl/" + href);
    InputSource xslInputSource = new InputSource(in);
    Document xslDoc = dBuilder.parse(xslInputSource);
    DOMSource xslDomSource = new DOMSource(xslDoc);
    xslDomSource.setSystemId("xsl/" + href);
    return xslDomSource;
 } catch (...

And assigning this with the TransformerFactory

tFactory.setURIResolver(new MyURIResolver());

Answered by: Melissa556 | Posted: 22-02-2022

Answer 2

URIResolver can also be used in a more straightforward way as below:

class XsltURIResolver implements URIResolver {

    public Source resolve(String href, String base) throws TransformerException {
              InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("xslts/" + href);
              return new StreamSource(inputStream);
        catch(Exception ex){
            return null;

Use the URIResolver with TransformerFactory as shown below:

TransformerFactory transFact = TransformerFactory.newInstance();
transFact.setURIResolver(new XsltURIResolver());

Or with a lambda expression:

transFact.setURIResolver((href, base) -> {
    final InputStream s = this.getClass().getClassLoader().getResourceAsStream("xslts/" + href);
    return new StreamSource(s);

Answered by: Lenny291 | Posted: 22-02-2022

Answer 3

Set your DocumentBuilder object with an EntityResolver.

You'll have to extend EntityResolver class to resolve your external entities (footer.xsl and topbar.xsl).

Answered by: Brad297 | Posted: 22-02-2022

Answer 4

I had a problem similar to this once with relative paths in the XSLT.

If you can, try to put absolute paths in the XSLT - that should resolve the error.

An absolute path probably isn't preferable for the final version of the XSLT, but it should get you past the maven problem. Perhaps you can have two versions of the XSLT, one with absolute paths for maven and one with relative paths for whatever other tool it's being used with.

Answered by: Kate797 | Posted: 22-02-2022

Similar questions

java - Using 3d transformation matrices

In an AI class we have a robot that has an arm with 7 joints. Each joint can rotate a different direction. I need to know where the very end is going to be. I've been trying to do 3d matrix multiplication and it works for one joint but once I add in another it doesn't line up with a model I made using the Java3D api. This is how I'm trying to calculate the position of the second joint. double[][] b = {{...

Perform xml transformation and filtering in java

I'd like to filter a couple of nested xml elements, evaluation of their attributes. For this purposes I'm searching a efficient and lightweight java api or framework. The main requirements are: filtering of element bodies, based on some pattern or condition event based XML transformation My first idea was apache jelly, but jelly has an ungly side effect. It removes CDATA tags...

java - Transformation error: "The current event is not START_ELEMENT but 2"

Similarly to an older post I'm trying to access a web service with JAX-WS using: Dispatch<Source> sourceDispatch = null; sourceDispatch = service.createDispatch(portQName, Source.class, Service.Mode.PAYLOAD); Source result = sourceDispatch.invoke(new StreamSource(new StringReader(req))); System...

java - Generating XHTML from Servlet after XSL Transformation

I've written a servlet that takes some xml and xsl and produces a PDF, using ITextRenderer, and displays it on the screen. However, what I wish to do now is to display the resulting XHTML from the transformation on a webpage. I've created a new servlet which gets the xml and xsl as normal, but I'm unsure how to get the resultant XHTML string. I've had a look online and tried using the javax.xml.transform.Tr...

What XSLT processor should I use for Java transformation?

What XSLT processor should I use for Java transformation? There are SAXON, Xalan and TrAX. What is the criteria for my choice? I need simple transformation without complicated logical transformations. Need fast and easy to implement solution. APP runs under Tomcat, Java 1.5. I have some jaxp libraries and could not understand what is the version of jaxp used. Thanks in advance. Best regards.

java - How to modularize a b2b webservice transformation application

How would you modularize a large application that has some incoming (SOAP) webservices, some outgoing webservices, transformations between them and internal formats, internal logging services, accesses external archiving webservices, delays stuff and works on this asynchronously and so forth? One way is to split the functionality into a collection of WAR, deploy all of them on one application server and have them c...

xslt - Java / XSL: Transformation result different between jre 1.5 and jre 1.6

I have just been working on an old Java app and switched the jre from 1.5 to 1.6. The app uses xsl to transform xml to html and this had been working fine until I changed the jre. Here is a extract from the xsl and xml: XML <link href="Umläut.txt" target="_blank"> <style tag="text">Umläut.txt</style> </link> XSL <...

java - Data transformation

I have data composed of a list of employers and a list of workers. Each has a many-to-many relationship with the other (so an employer can have many workers, and a worker can have many employers). The way the data is retrieved (and given to me) is as follows: each employer has an array of workers. In other words: employer n has: worker x, worke...

Java image transformation

I need to create 3d depth effect for my image. Number 1 is what I have and number 2 is shape where I want to transform number 1. So is there any method for that in Java standard graphics libraries or some other open source libraries?

java - How to change JUnit transformation in Maven2

I have Java project built with Maven2. There is used JUnit framework for testing ( and Selenium but it is irrelevant ). I would like to add screenshot ( I have it ) into result of tests. Here, on SO, I found similar question which solves it but with Ant. I would like to know if there is any option how to manage it with Maven2 instead of Ant or if...

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)