Friday, April 25, 2008

Relative paths and the document() function

A nice gotcha cropped up today on xsl-list...

Relative paths passed to the document() function are resolved against either the XML or the stylesheet depending on what is passed in: a node from the XML will mean the path is resolved against the XML, a string will mean it's resolved against the stylesheet.

The gotcha is this - if you modify this:


to this:

<xsl:variable name="path" select="@path" as="xs:string"/>

and @path contains a relative path, then you could get a document not found error, or worse if your XML and XSLT are in the same directory, you won't notice...


David said...

You should probably mention the workaround to use document($path,.) instead.

Normally I find this hits when for example the source doesn't include an extension so you want to use document(concat(@file,'.xml')) which has the same effect of making it a string rather than a source document node.

Stefanie said...

Big thanks to you David and Andrew! This workaround is just what I needed.