views:

44

answers:

2

I'm looking for a concrete example of an XSL stylesheet storing the current working directory into a variable.

I need this because in my situation, I need to import certain library stylesheets using a relative path. Knowing the path my processor is choosing as the current directory would be sufficient.

EDIT

Nothing vendor specific please.

+1  A: 

In the XSL world there's no guarantee that an absolute current working directory exists, or even that the concept has meaning. Any answer to this question would necessarily be vendor-specific.

Jim Garrison
@Jim-Garrison: Actually, your answer generally isn't true.
Dimitre Novatchev
@Dimitre: Could you elaborate a little?
Jim Garrison
@Jim-Garrison: What the OP is actually asking is the *base-uri* of the xslt stylesheet module, not the working directory.
Dimitre Novatchev
+2  A: 

In XSLT 2.0 one can use the standard XPath 2.0 function resolve-uri().

Do note, that the relative URIs of the included/imported stylesheet modules are relative to the base URI of the including/importing stylesheet module -- not from the "working directory"!

Here is part of the description of this function from the W3 F&O specification:

8.1 fn:resolve-uri

fn:resolve-uri($relative as xs:string?) as xs:anyURI?

fn:resolve-uri($relative as xs:string?, $base as xs:string) as xs:anyURI?

Summary: The purpose of this function is to enable a relative URI to be resolved against an absolute URI.

The first form of this function resolves $relative against the value of the base-uri property from the static context. If the base-uri property is not initialized in the static context an error is raised [err:FONS0005].

Here is a very simple example:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:output method="text"/>

 <xsl:template match="/">
  <xsl:sequence select=
   "resolve-uri('resolve-uri-example2.xsl')"/>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied against any xml document (not used), the result is:

file:///c:/tests/resolve-uri-example2.xsl

This is the correct result, because our main stylesheet module is saved as:

c:/tests/resolve-uri-example2.xsl
Dimitre Novatchev
thanks dimitre, could you please provide a concrete example of using it to determine the current path. i can't see how to do this from the documentation
Mike
@Mike: Yes, I updated my answer with an example.
Dimitre Novatchev
Thank you dimitre, as always I appreciate your thorough help with my xslt issues.
Mike
@Mike: You are welcome. Hope my answer has been useful to the extend that you would upvote and accept it. :)
Dimitre Novatchev