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:

document(@path)

to this:

<xsl:variable name="path" select="@path" as="xs:string"/>
...
document($path)


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...

3 comments:

David Carlisle 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.

Unknown said...

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

Guitar Bookmarks said...

Finding this yesterday would have saved me roughly 8 hours of productivity. Ah well. Thanks for the help!