我必须将节点元素的数据从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