9

The Saxon processor gives me an error whenever I have an XSLT import statement. Here is the error:

XTSE0165: I/O error reported by XML parser processing file: shared/test.xslt (The system cannot find the path specified):

Here is how my XSLT document looks like:

<?xml version='1.0' encoding='UTF-8'?>

<xsl:stylesheet version='2.0' 
    xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
    xmlns:fn='http://www.w3.org/2005/02/xpath-functions'
    xmlns:xs='http://www.w3.org/2001/XMLSchema'
    >

    <xsl:import href="shared/test.xslt"/>

...

My java code

TransformerFactory transformerFactory = TransformerFactoryImpl.newInstance();

transformerFactory.setURIResolver(uriResolver);  //my own custom URI resolver

Transformer transformer = transformerFactory.newTransformer(new StreamSource(xsltInputStream));   //this is where the error occurs when I debug!

The URI resolver class is never triggered! It chocks up on the newTransformer() method above.... I tried XsltCompiler, etc and same thing... If I remove the import statement, everything works!! It can't find the file to import which is fine but that's why I have the resolver class to help it locate the file but it never triggers the resolver and fails locating the file to import!

How do I resolve this?

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
Ayyoudy
  • 3,641
  • 11
  • 47
  • 65

1 Answers1

11

You likely need to set the System ID for StreamSource of the XSLT that you are loading.

When you load from a StreamSource, it doesn't know where your XSLT "lives" and has difficulty determining how to resolve relative paths.

StreamSource source = new StreamSource(xsltInputStream);
source.setSystemId(PATH_TO_THE_XSLT_FILE_ON_THE_FILESYSTEM_OR_URL);
Transformer transformer = transformerFactory.newTransformer(source); 
james.garriss
  • 12,959
  • 7
  • 83
  • 96
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
  • Correct answer. xsl:import could probably be made to work when there is no base URI provided there is a URIResolver, but only with difficulty, because of the rule that says if you have two imports for the same absolute URI, you get the same module back. – Michael Kay Aug 30 '11 at 08:04
  • @Mads Hansen, thank you. That worked. Still odd however that the URIResolver was not even triggered. – Ayyoudy Aug 30 '11 at 14:19
  • but what to do if the xslt file in under resources in a jar file? I am not getting it running properly... – YaP Oct 25 '17 at 16:34