2009-02-13 137 views
12

有没有办法从xslt文件中获取当前文件夹路径?xslt获取文件当前文件夹路径

需要它来查找其他xml和xslt文件。我们有不同的客户文件夹,需要成功找到正确的文件。

干杯

回答

6

您可以使用xsl:param从外部将它发送到样式表中。然后你需要确定从外部调用当前的路径是什么;)

3

没有...
但你也许可以解决方法通过使用相对URL和/或参数传递到样式表的问题。

1

不AFAIK(虽然你总是可以将它作为参数传递给变换),但我不清楚为什么相对路径赢得'在这里为你工作。

4

在Windows上MSXSL,您可以使用脚本扩展这样的:

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

其他XSL处理器支持类似的方法来使用外部资源(脚本语言,函数库等),所以这只是一个例。

+0

不知道为什么这是被投票。它实际上工作。 – Tomalak 2009-02-13 14:59:58

+2

对不起。我投票选择使用非标准扩展名,但在添加我的答案后,我意识到我提出了同样的问题。我认为没有“标准”的方式来做到这一点。 (除了使用参数) – 2009-02-13 15:05:00

2

在大多数XSLT处理器中,您可以添加自定义函数作为扩展。例如这里是Saxon's documentation该怎么做。

4

有没有办法从xslt文件中获取当前 文件夹路径?

需要它来寻找其他XML和XSLT 文件

无需任何扩展功能,甚至参数来做到这一点!

<xsl:import><xsl:include> 指令的href属性使用任何相对 URL被基于当前XSLT样式表的URL解析 - 它仅需要具有一个URL,它是vlearly表述为在上面的问题中是真的。这在导入/包括XSLT样式表时非常方便。

document()功能也会以类似的方式解决相对URL,因此利用anrelative URL进行任何额外的XML文档进行访问。

最后,这里是an example如何设施,在一个大库的XSLT的功能和模板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> 
4

这可能是你的设置工作:

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

例如,

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