1

I'm using a class (Saxon xslt) that requires a number of XML style sheets be loaded:

import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.Serializer;
import net.sf.saxon.s9api.XdmNode;
import net.sf.saxon.s9api.XsltCompiler;
import net.sf.saxon.s9api.XsltExecutable;
import net.sf.saxon.s9api.XsltTransformer;    

XsltCompiler comp = (new Processor(false)).newXsltCompiler();

ClassLoader classloader = Thread.currentThread().getContextClassLoader();
InputStream is = classloader.getResourceAsStream("xsl_files/main.xsl");

XsltExecutable exp exp = comp.compile(new StreamSource(is));

xsl_files is in the resource folder. main.xsl is loaded correctly, but main.xsl references other files that is in the xsl_files folder needed by Saxon's XsltCompiler but that isn't loaded from java resources. If I copy xsl_files into a physical folder on the hard drive, then I can simply use the following and all the files main.xsl uses is loaded correctly:

XsltExecutable exp = comp.compile(new StreamSource(new File("path/to/xsl_files/main.xsl")));

Any way around this to load the entire folder from java resources instead of a real path on the hard drive?

leontp587
  • 791
  • 2
  • 9
  • 21

1 Answers1

2

Use http://saxonica.com/html/documentation/javadoc/net/sf/saxon/s9api/XsltCompiler.html#setURIResolver%28javax.xml.transform.URIResolver%29 to provide a URIResover that knows to load from your resource folder, there are some answers showing how to implement that, like Resolving relative paths when loading XSLT files and https://stackoverflow.com/a/12453881/252228.

Community
  • 1
  • 1
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110