c00ke
c00ke

Reputation: 2315

How do I get the current folder path within an XSLT file?

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

I need it to locate other XML and XSLT files. We have different customer folders and will need to successfully find the correct files.

Upvotes: 18

Views: 25335

Answers (7)

Matthew Simoneau
Matthew Simoneau

Reputation: 6279

This may work for your setup:

<xsl:value-of select="system-property('user.dir')"/>

For example,

<xsl:value-of select="document(concat(system-property('user.dir'),'/',filename,'.xml'))//title[1]"/>

Upvotes: 6

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243449

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>

Upvotes: 7

Tomalak
Tomalak

Reputation: 338208

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.

Upvotes: 6

Peter Štibran&#253;
Peter Štibran&#253;

Reputation: 32893

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

Upvotes: 3

annakata
annakata

Reputation: 75804

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.

Upvotes: 2

krosenvold
krosenvold

Reputation: 77151

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 ;)

Upvotes: 7

jor
jor

Reputation: 711

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

Upvotes: 4

Related Questions