如何使用XSLT合并两个xml文件
发布时间:2020-12-16 07:43:48 所属栏目:百科 来源:网络整理
导读:我有两个xml文件需要通过使用XSLT合并为一个. 第一个XML是(原始的): feed author firstNamef/firstName lastNamel/lastName /author date2011-01-02 /date entry id1/id Nameaaa/Name ContentXXX/Content /entry entry id2/id Namebbb/Name ContentYYY/Cont
我有两个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> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |