0
votes

During a Maven build, configured to use the Maven XML Plugin, a XSL stylesheet using the document() function to load a sibling XML document fails to find the file using relative reference and generates an error. I'm using Saxon-HE 9.7.0-14 as the XSLT processor.

While transforming file-1.xml using a XSL stylesheet that uses <xsl:value-of select="document('file-2.xml')/someElementTag"> the following error occurs:

Recoverable error FODC0002: I/O error reported by XML parser processing file:/C:/MyWorkspace/transform/src/main/resources/stylesheets/file-2.xml: C:\MyWorkspace\transform\src\main\resources\stylesheets\file-2.xml (The system cannot find the specified file)

How can I configure Maven XML Plugin with a SystemID or URIResolver to resolve the relatives document references?

1
The error message suggests the relative URI file-2.xml was resolved to file:/C:/MyWorkspace/transform/src/main/resources/stylesheets/file-2.xml so it is not a problem of not having any base URI. Does using document('file-2.xml', /) give you the right location (resolve relative to the base URI of the input document)? - Martin Honnen
Works like a charm. Can you post this as an answer? - Renan Mozone
I have written an answer with the suggestion and the spec text explaining the second argument of the document function for resolving URIs. - Martin Honnen

1 Answers

1
votes

The error message suggests that the XSLT processor could resolve the relative URI passed in, if you read https://www.w3.org/TR/xslt#document which says

The URI reference may be relative. The base URI (see [3.2 Base URI]) of the node in the second argument node-set that is first in document order is used as the base URI for resolving the relative URI into an absolute URI. If the second argument is omitted, then it defaults to the node in the stylesheet that contains the expression that includes the call to the document function.

then you will see that it resolved the relative URI relative to the stylesheet URI. To resolve relative to the base URI of your input document's root node you can use document('file-2.xml', /) instead of document('file-2.xml').