xslt get the file current folder path

2019-02-06 04:19发布

问题:

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

回答1:

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



回答2:

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>


回答3:

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.



回答4:

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]"/>


回答5:

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



回答6:

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



回答7:

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.



标签: xslt