2009-02-13 144 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]"/>