Maintenance: Planio will be observing a scheduled maintenance window this Tuesday, November 5, 2024 from 03:00 UTC until 06:30 UTC to perform urgent network maintenance in our primary data center. Your Planio account will be unavailable during this maintenance window.
Name resolution in stylesheet include
Added by evan massena over 12 years ago
Hello.
I have the following Java code (Transformation is just a convenience class that holds a set of paths as strings, it's not important):
Transformation t; this.style_stream = new FileInputStream(transformation.getStylesheetFile()); this.style_source = new StreamSource(this.style_stream); this.input_stream = new FileInputStream(transformation.getDocumentFile()); this.input_source = new StreamSource(this.input_stream); this.output_file = transformation.getOutputDirectory() + File.separatorChar + transformation.getOutputFile(); this.output_stream = new FileOutputStream(this.output_file); this.output_serial = new Serializer(this.output_stream); this.config = new Configuration(); this.config.setXIncludeAware(true); this.processor = new Processor(this.config); this.xslt = this.processor.newXsltCompiler(); this.xslt_exec = this.xslt.compile(this.style_source); this.xslt_trans = this.xslt_exec.load(); this.xslt_trans.setDestination(this.output_serial); this.xslt_trans.setSource(this.input_source); this.xslt_trans.transform();
The stylesheet in question contains xsl:include elements that contain relative paths in their href attributes. Unfortunately, the code above simply results in an error claiming that the stylesheet referenced in the include can't be found, and the actual error suggests that it's attempting to resolve the include relative to the current working directory of the program. The command line saxon tool appears to resolve paths relative to the actual files themselves, given the exact same initial working directory, input files, and stylesheet. I'm assuming that my code above is doing something obviously wrong.
I should mention that XIncludes in input documents are actually resolved correctly, but I'm not sure if that's relevant.
Any ideas?
Replies (5)
Please register to reply
RE: Name resolution in stylesheet include - Added by evan massena over 12 years ago
Actually, I tell a lie. The command line saxon tool appears to resolve XIncludes relative to the current working directory, but resolves stylesheet includes relative to the files themselves.
RE: Name resolution in stylesheet include - Added by evan massena over 12 years ago
Nope! I was right the first time, both XIncludes and stylesheet includes are resolved relative to the files themselves. Not the current working directory.
Sorry, was looking at the wrong test output...
RE: Name resolution in stylesheet include - Added by Michael Kay over 12 years ago
A relative URI in xsl:include is resolved relative to the base URI of the xsl:include element. Under normal circumstances this will be the URL/filename of the file containing the stylesheet module. If this is unknown, for example because the stylesheet module is supplied as a DOM, or as a Java InputStream with no associated SystemId, then Saxon defaults the base URI to the current working directory.
RE: Name resolution in stylesheet include - Added by Michael Kay over 12 years ago
In your Java code you are supplying an StreamSource containing an InputStream and a null SystemID. Saxon therefore has no idea where the data was read from, and defaults the location to the current working directory.
The simplest solution here is to use new StreamSource(File), specifically
this.style_source = new StreamSource(transformation.getStylesheetFile());
which creates a StreamSource with a known base URI.
RE: Name resolution in stylesheet include - Added by evan massena over 12 years ago
Ah, yes, that makes sense. Thanks very much.
Please register to reply