【问题标题】:how to Merge two xml files with XSLT如何使用 XSLT 合并两个 xml 文件
【发布时间】:2013-03-04 04:46:36
【问题描述】:

我有两个需要使用 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,但仍然找不到答案。感谢您的帮助。

【问题讨论】:

  • 在不使用扩展的情况下,在 XSLT1 中很强大;在 XSLT2 中微不足道。您使用的是哪种 XSLT 处理器,它是否支持 XSLT2?
  • 嗨 Jim,我的项目中使用了 xslt1。谢谢。
  • 当你搜索stackoverflow时,你可能找到your own question that was answered yesterday吗?
  • @JLRishe 是的,我昨天问了类似的问题,但是这个问题与前一个问题略有不同。实际上每个 xml 文件都有一个标题部分,也需要合并。我是 XSLT 的新手,请帮忙,谢谢。
  • 如果这是关键区别,那么我认为在您发布这个新问题而不是 5 小时后包含它是有意义的。 :)

标签: xml xslt


【解决方案1】:

与我为您上一个问题提供的答案几乎相同,已修改以匹配您的新 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>

【讨论】:

  • 谢谢你,你是生命的救星!怎么还有点问题。实际上,在所有需要合并的条目之前都有一个标题。我已经修改了原始问题。
  • JLRishe, @JLRishe 除了标题部分外,您的解决方案效果很好。对不起,我的描述不完整,我试图弄清楚,但没有奏效。请帮忙。非常感谢。
  • 我已经更新了上面的答案。您还需要合并标头值,还是只使用原始 XML 中的值?
  • 谢谢你,JLRishe,@JLRishe。我的问题解决了。你是我的英雄!
  • @Lijo 这个问题与 C# 无关,所以我不会编辑我的答案来为您提供 C# 代码,但希望这会有所帮助:google.co.jp/…
猜你喜欢
  • 2013-10-02
  • 1970-01-01
  • 1970-01-01
  • 2019-08-25
  • 1970-01-01
  • 2019-01-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多