tags:

views:

1962

answers:

6

Is there a way to get the current folder path from within a xslt file?

Need it to locate other xml and xslt files. We have different customer folders and will need to successfully find the correct files.

Cheers

+3  A: 

no...
but you could maybe workaround the problem by using relative URLs and/or passing parameters into the stylesheet.

jor
+3  A: 

You can send it into the style-sheet from outside using xsl:param. Then you need to determine what the current path is when invoking the from the outside ;)

krosenvold
+1  A: 

Not AFAIK (though you could always pass it as a param to the transform), but I'm not clear why relative paths won't work for you here.

annakata
+3  A: 

In MSXSL on Windows, you can use a script extension like this:

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  xmlns:user="http://tempuri.org/msxsl"
>

  <msxsl:script language="JScript" implements-prefix="user">
<![CDATA[
var fso = new ActiveXObject("Scripting.FileSystemObject");

function getCurrentPath(){
  return fso.GetFolder(".").Path
}
]]>
  </msxsl:script>

  <xsl:template match="/">
    <xsl:value-of select="user:getCurrentPath()"/>
  </xsl:template>

</xsl:stylesheet>

Other XSL processors support similar methods to use external resources (scripting languages, function libraries etc.), so this is just an example.

Tomalak
Not sure why this was down voted. It actually works.
Tomalak
Sorry for that. I voted it down for using non-standard extension, but after adding my answer, I realized that I suggested the same thing. I think there is no 'standard' way to do it. (Except using params)
Peter Štibraný
Thanks for explaining your reasoning, much appreciated. :-)
Tomalak
+2  A: 

In most XSLT processor, you can add custom functions as extensions. For example here is Saxon's documentation how to do that.

Peter Štibraný
+1  A: 

Is there a way to get the current folder path from within a xslt file?

Need it to locate other xml and xslt files

No need for any extension functions or even parameters to do that!

Any relative URLs used in the href attribute of an <xsl:import> or <xsl:include> instruction are resolved based on the URL of the current XSLT stylesheet -- it only needs to have an URL, which is vlearly stated as true in the question above. This is very convenient in importing/including XSLT stylesheets.

The document() function also will resolve a relative URL in a similar way, thus making any additional XML document accessible using anrelative URL.

Lastly, here is an example how this facilities are massively used in a big library of XSLT functions and templates (FXSL 2.x):

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:f="http://fxsl.sf.net/"
 exclude-result-prefixes="xs xdt f"
>
<!--
       This module contains the FXSL versions of the "standard" XPath functions

       These are intended as convenience functions, so that they can be passed 
       as parameters to other functions (e.g. to f:zipWith())                  
       or curried and passed as parameters (e.g. to f:map())                   
-->

 <xsl:import href="func-curry.xsl"/>
 <xsl:import href="func-compose-flist.xsl"/>

 <xsl:import href="func-standardArithmeticXpathFunctions.xsl"/>
 <xsl:import href="func-standardBooleanXpathFunctions.xsl"/>
 <xsl:import href="func-standardStringXpathFunctions.xsl"/>
 <xsl:import href="func-standardNodesXpathFunctions.xsl"/>
 <xsl:import href="func-standardSequencesXpathFunctions.xsl"/>
 <xsl:import href="func-standardAggregateXpathFunctions.xsl"/>
 <xsl:import href="func-standardDateTimeXpathFunctions.xsl"/>
 <xsl:import href="func-standardXSLTXpathFunctions.xsl"/>
 <xsl:import href="func-standardAxisXpathFunctions.xsl"/>

</xsl:stylesheet>
Dimitre Novatchev
You're absolutely right about imports, includes, document and relative paths. Extensions or parameters were suggested for getting current directory only.
Peter Štibraný