2011-05-07 140 views
1

我需要使用XSLT將幾個XML文件合併爲一個。我有4個XML文件發佈,接收,theatre1和theatre2。版本必須先被添加,然後其匹配的接收必須被放置在版本部分的內部。其他兩個應該被添加。如何使用xslt將幾個xml文件合併到一個xml文件中?

這裏是文件的格式。 發佈: 文本

接待: 文本

的結果應該是: < 文本 文本

這裏是我到目前爲止,但它不能正常工作完全

其他2個文件只需要在最後

+0

您沒有提供任何的XML文件!請改正。 – 2011-05-07 16:14:09

回答

2

Reading Multiple Input Documents似乎回答這個問題以復加。

當你運行一個XSLT處理器,你告訴它在哪裏可以找到源代碼樹中的文件 - 可能是在本地或遠程計算機上的磁盤文件 - 和樣式表適用於它。您不能讓處理器一次將樣式表應用於多個輸入文檔。然而,document()函數允許樣式表命名一個附加文檔來讀入。您可以將整個文檔插入到結果樹中,或者根據XPath表達式描述的條件插入其中的一部分。您甚至可以使用xsl:key指令和key()函數在源文檔之外的文檔中查找關鍵值。

因此,在xslt中加載多個文檔的關鍵是使用document()函數。

3

下面是如何進行的:

$ expand -t2 release.xml 
<release name="bla"/> 

$ expand -t2 reception.xml 
<receptions> 
    <reception name="bla"> 
    <blabla/> 
    </reception> 
    <reception name="blub"> 
    <blubbel/> 
    </reception> 
</receptions> 

$ expand -t2 theatre1.xml 
<theatre number="1"/> 

$ expand -t2 theatre2.xml 
<theatre number="2"/> 

$ expand -t2 release.xsl 
<?xml version="1.0" encoding="ISO-8859-1"?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:strip-space elements="*"/><!-- make output look nice --> 
    <xsl:output indent="yes"/> 

    <xsl:template match="release"> 
    <xsl:variable name="rel-name" select="@name"/> 
    <xsl:copy> 
     <xsl:copy-of select="node()"/><!-- copy remainder of doc --> 
     <xsl:copy-of select="document('release.xml')"/> 
     <xsl:variable name="rcpt-doc" select="document('reception.xml')"/> 
     <xsl:copy-of select="$rcpt-doc/*/reception[ @name = $rel-name ]"/> 
     <xsl:copy-of select="document('theatre1.xml')"/> 
     <xsl:copy-of select="document('theatre2.xml')"/> 
    </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

這樣稱呼它:

xsltproc release.xsl release.xml 

這是結果:

<?xml version="1.0"?> 
<release> 
    <release name="bla"/> 
    <reception name="bla"> 
    <blabla/> 
    </reception> 
    <theatre number="1"/> 
    <theatre number="2"/> 
</release> 
相關問題