我必須將節點元素的數據從file1.xml複製到file2.xml。 file1.xml使用XSLT將數據從一個XML文檔複製到另一個
<?xml version="1.0" encoding="utf-8" ?>
<root>
<header>
<AsofDate>31-Dec-2012</AsofDate>
<FundName>This is Sample Fund</FundName>
<Description>This is test description</Description>
</header>
</root>
file2.xml
<?xml version="1.0" encoding="utf-8" ?>
<root id="1">
<header id="2">
<AsofDate id="3"/>
<FundName id="4" />
<Description id="5" />
</header>
</root>
合併file1.xml到file2.xml後,結果應該看看下面:
<?xml version="1.0" encoding="utf-8" ?>
<root id="1">
<header id="2">
<AsofDate id="3">31-Dec-2012</AsofDate>
<FundName id="4">This is Sample Fund</FundName>
<Description id="5">This is test description</Description>
</header>
</root>
我使用在XSLT下面轉換文件。
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
下面是代碼用於執行變換:
XslCompiledTransform tf = new XslCompiledTransform();
tf.Load("TranFile.xsl");
tf.Transform("file1.xml", "file2.xml");
但上面的代碼重寫file2的內容與內容file1.xml。這只是示例XML。在實際情況下,我們不知道XML文件的節點和層次結構的名稱。但是無論哪種結構對於文件和場景都是一樣的,都是完全相同的。我是XSLT新手,不確定這是否是正確的方法來完成結果。是否真的有可能通過XSLT實現結果。
的可能重複的[XSLT:一個簡單的方式來合併XML文件(http://stackoverflow.com/questions/1510688/xslt-a-simple-way-to-merge-xml-files) – iTech 2013-02-20 20:18:43
這裏我不知道來自服務的XML結構。 – Nps 2013-02-20 20:26:06
我想,在真實情況下,file2.xml中的id不會是順序的,不是?我的意思是你會發現像 ...順便說一句,你是否嘗試使用文檔('')函數? –
2013-02-20 22:54:11