2013-03-04 72 views
6

我有两个xml文件需要通过使用XSLT合并成一个文件。如何用XSLT合并两个xml文件

首先XML是(原来的):

<feed> 
    <author> 
    <firstName>f</firstName> 
    <lastName>l</lastName> 
    </author> 
    <date>2011-01-02 </date> 
    <entry> 
    <id>1</id> 
    <Name>aaa</Name> 
    <Content>XXX</Content>  
    </entry> 
    <entry> 
    <id>2</id> 
    <Name>bbb</Name> 
    <Content>YYY</Content> 
    </entry> 
</feed> 

第二XML(更新后的数据)是这样的:

<feed> 
     <author> 
     <firstName>f</firstName> 
     <lastName>l</lastName> 
     </author> 
     <date>2012-05-02 </date> 
     <entry> 
     <id>2</id> 
     <Name>newName</Name> 
     <Content>newContent</Content>  
     </entry> 
     <entry> 
     <id>3</id> 
     <Name>ccc</Name> 
     <Content>ZZZ</Content> 
     </entry> 
    </feed> 

期望合并结果 - 使用所述第二XML来更新第一一个:

<feed> 
     <author> 
     <firstName>f</firstName> 
     <lastName>l</lastName> 
     </author> 
     <date>2012-05-02 </date> 
     <entry> 
     <id>1</id> 
     <Name>aaa</Name> 
     <Content>XXX</Content>  
     </entry>  
     <entry> 
     <id>2</id> 
     <Name>newName</Name> 
     <Content>newContent</Content>  
     </entry> 
     <entry> 
     <id>3</id> 
     <Name>ccc</Name> 
     <Content>ZZZ</Content> 
     </entry> 
    </feed> 

我已经搜索了stackoverflow,但仍然找不到答案。感谢帮助。

+0

在不使用扩展名的情况下在XSLT1中很难做到;在XSLT2中微不足道。你在使用哪种XSLT处理器?它支持XSLT2吗? – 2013-03-04 04:57:42

+0

嗨吉姆,xslt1用于我的项目。谢谢。 – skyfree 2013-03-04 05:01:47

+1

当你搜索stackoverflow你有没有发现[你自己的问题昨天回答](http://stackoverflow.com/q/15175287/1945651)? – JLRishe 2013-03-04 07:15:22

回答

9

差不多,我给你的最后一个问题,修改,以符合新的XML格式提供了相同的答案:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes"/> 
    <xsl:param name="fileName" select="'updates.xml'" /> 
    <xsl:param name="updates" select="document($fileName)" /> 

    <xsl:variable name="updateItems" select="$updates/feed/entry" /> 

    <xsl:template match="@* | node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@* | node()"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="feed"> 
    <xsl:copy> 
     <xsl:apply-templates select="@* | node()[not(self::entry)] | 
            entry[not(id = $updateItems/id)]" /> 
     <xsl:apply-templates select="$updateItems" /> 
    </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

当第一个样本XML运行,与第二个保存为“updates.xml “,产生如下结果:

<feed> 
    <author> 
    <firstName>f</firstName> 
    <lastName>l</lastName> 
    </author> 
    <date>2011-01-02 </date> 
    <entry> 
    <id>1</id> 
    <Name>aaa</Name> 
    <Content>XXX</Content> 
    </entry> 
    <entry> 
    <id>2</id> 
    <Name>newName</Name> 
    <Content>newContent</Content> 
    </entry> 
    <entry> 
    <id>3</id> 
    <Name>ccc</Name> 
    <Content>ZZZ</Content> 
    </entry> 
</feed> 
+0

谢谢你,你是一个救世主!如何还有一点问题。实际上,在所有需要合并的条目之前都有一个标题。我修改了原始问题。 – skyfree 2013-03-04 09:36:39

+0

JLRishe,@JLRishe您的解决方案非常适用于标题部分。对于我不完整的描述感到抱歉,我试图找出它,但没有奏效。请帮忙。非常感谢。 – skyfree 2013-03-04 09:47:30

+0

我已经更新了我上面的答案。你是否还需要合并标题值,还是只使用原始XML中的值? – JLRishe 2013-03-04 11:01:27