2013-08-27 40 views
3

我即將合併XML文件(並添加元信息),其相對路徑在我的輸入XML文件中指定。我要合併的文件位於一個名爲「文件」的子目錄 輸入文件的結構如下XSLT打開其他xml文件

<files> 
    <file> 
     <path>files/firstfile.xml</path> 
    </file> 
    <file> 
     <path>files/secondfile.xml</path> 
    </file> 
</files> 

的firstfile.xml和secondfile.xml具有以下結構

<tables> 
     <table name = "..."> 
     ... 
     </table> 
     ... 
    <tables> 

我想將一個文件的所有表節點放在一個組中,並向它添加元信息。所以我寫了下面的XSLT樣式表:

<xsl:template match="/"> 
    <tables> 
      <xsl:apply-templates/> 
    </tables> 
</xsl:template> 


<xsl:template name = "enrichWithMetaInformation" match = "file"> 
      <xsl:apply-templates select="document(./path)/tables"> 
       <xsl:with-param name="file" select="."/> 
      </xsl:apply-templates> 


</xsl:template> 

<xsl:template match="tables"> 

    <group> 
     <meta> 
      ...Some meta data ... 
     </meta> 
     <xsl:copy-of select="./table"/> 
    </group> 
</xsl:template> 

對於每一個文件,我得到一個錯誤:

The system could not find the file specified.

它指出,空節點集已經返回(這樣的文件無法加載)。有沒有人知道如何解決這個問題?

Cheers

+0

@IanRoberts,當document()的參數是一個節點時,路徑將相對於該節點的基本URI進行解釋。 –

+0

@MichaelKay我站在糾正,我發現規範的形式語言,而不是有時難以解析...... –

回答

2

檢查源文檔的基本URI是否已知,例如通過執行base-uri(/)。在參數作爲節點(或節點集合)提供的情況下,該值用於解析傳遞給document()的相對URI。其中基礎URI是未知

兩種常見的情況是:

的(a)如果提供了源文檔作爲內存中的DOM

(b)中,如果你提供的源文件作爲輸入流或讀者沒有已知的URI。

+0

我該如何做到這一點(我是新來的主題) 像這樣: 或者我能夠在樣式表頭中聲明它嗎? 這是真的,這是唯一可能的XSLT 2.0(我必須使用XSLT 1.0)有人寫在這裏: http://www.stylusstudio.com/xsllist/200305/post70610.html – slashburn

+0

對不起,base-uri( )是一個2.0函數。但是,如果你告訴我們源文件是如何提供的,那應該很明顯。 –

+1

源文檔作爲內存中的DOM提供 – slashburn

1

document()函數的參數應該是一個字符串。在./中獲取變量的路徑節點值並將其傳入。當我到達計算機時,我可以做一個快速測試,但我認爲這是您的問題。以下是一個選項:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
version="1.0"> 
<xsl:template match="/"> 
    <tables> 
     <xsl:apply-templates/> 
    </tables> 
</xsl:template> 
<xsl:template match = "file"> 
    <xsl:variable name="filepath" select="concat('./',path)"/> 
    <xsl:call-template name="tableprocessor"> 
     <xsl:with-param name="tables" select="document($filepath)/tables"/> 
    </xsl:call-template> 
</xsl:template> 
<xsl:template name="tableprocessor"> 
    <xsl:param name="tables"/> 
    <group> 
     <xsl:copy-of select="$tables"/> 
    </group> 
</xsl:template> 
</xsl:stylesheet> 
+0

當它的第一個參數是一個節點集,它''文件工作正常,它返回節點集的聯合,從依次調用'document'和每個節點的字符串值。 –

+0

是的,這也是正確的。我發佈後,我也做了測試。我正在使用一個變量來方便調試。說,上述XSL工作正常,處理完全按照預期的樣本。 –