[英]how to Merge two xml files with XSLT
我有兩個xml文件需要通過使用XSLT合並為一個。
第一個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但仍然找不到答案。 感謝幫助。
與我在上一個問題中提供的答案幾乎相同,修改后與您的新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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.