2012-05-17 92 views
0

在單個XSLT文件中是否有辦法先打開一個XML文件並使用子元素提取元素,然後將該元素填充到第二個XML文件中?從一個XML文件中提取元素並將其填充到另一個XML文件

如果它不能在一個XSLT中完成,那麼我選擇的語言將是VB腳本。

我在這裏看到很多不同的例子,但我只是困惑,並不瞭解他們中的大多數。

這裏是我的工作從XML1提取節點的XSL:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output 
     method="xml" 
     omit-xml-declaration="yes" 
     indent="yes" 
     encoding="ISO-8859-1"/> 
    <xsl:template match="/"> 
    <xsl:copy-of select="root/Collection/Item/." /> 
    </xsl:template> 
    </xsl:stylesheet> 

我要承擔由此產生的項目並將其追加到XML2在同一XPATH,根/集。

我有一個這個答案的腳本How to save XML to a file(謝謝你安德魯庫珀),將XML返回到一個memvar。

XML1:

<root> 
<collection1> 
    <item attr1, attr2...> ---this is what I am getting 
    <more> 
    </more> 
    </item> 
<collection1> 
<collection2> 
    <item attr1, attr2...> 
    <more> 
    </more> 
    </item> 
<collection2> 
... 
<collection_n> 
    <item attr1, attr2...> 
    <more> 
    </more> 
    </item> 
<collection_n> 
</root> 

XML2:

<root> 
<collection1> 
    <item attr1, attr2...> 
    <more> 
    </more> 
    </item> 
---- i want to insert node from XML1 here, only in collection1 
<collection1> 
<collection2> 
    <item attr1, attr2...> 
    <more> 
    </more> 
    </item> 
<collection2> 
... 
<collection_n> 
    <item attr1, attr2...> 
    <more> 
    </more> 
    </item> 
<collection_n> 
</root> 

因此新XML2:

<root> 
<collection1> 
    <item attr1, attr2...> 
    <more> 
    </more> 
    </item> 
    <item attr1, attr2...> 
    <more> 
    </more> 
    </item> 
<collection1> 
<collection2> 
    <item attr1, attr2...> 
    <more> 
    </more> 
    </item> 
<collection2> 
... 
<collection_n> 
    <item attr1, attr2...> 
    <more> 
    </more> 
    </item> 
<collection_n> 
</root> 

回答

2

您將使用document()函數加載外部文件,這樣你就可以將它們合併你正在進行轉換的那個人。或者,如果您在調用XSLT時將它們作爲DOM樹存儲在內存中,則可以將它們作爲節點集提供給模板參數(xsl:param)。

看看我在一週前回答的this question。正是關於如何合併位於同一合併點(認爲相同的XPath)的內容與多個文檔。

希望它有幫助。如果沒有,請詢​​問一個更具體的問題,關於什麼不明確,你有什麼嘗試,什麼不適合你

+0

謝謝。我沒有看到那一個。這些問題有很多,但並非所有問題都很容易找到。再次感謝。 – dashrader

相關問題