使用 xslt 比较两个 xml 文件 [英] Comparing two xml files using xslt
本文介绍了使用 xslt 比较两个 xml 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个 XML 文件:
I have two XML files :
文件a":
<?xml version="1.0"?>
<catalog>
<cd>d</cd>
<cd>e</cd>
<cd>f</cd>
<cd>c</cd>
</catalog>
文件b":
<?xml version="1.0"?>
<catalog>
<cd>a</cd>
<cd>b</cd>
<cd>c</cd>
</catalog>
我想将文件 b
与文件 a
进行比较,并获取那些仅存在于文件 b
中的记录.
I want to compare File b
against File a
and get those records which are only present in file b
.
即预期输出是:
<?xml version="1.0"?>
<catalog>
<cd>a</cd>
<cd>b</cd>
</catalog>
推荐答案
如果您可以使用 XSLT 2.0,那么可以通过使用 键 非常简单(有效)地完成此操作.假设您正在处理b"文件:
If you can use XSLT 2.0, then this can be done very simply (and efficiently) by using a key. Assuming you are processing the "b" file:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:key name="cd" match="cd" use="." />
<xsl:template match="/catalog">
<xsl:copy>
<xsl:copy-of select="cd[not(key('cd', ., document('a.xml')))]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
这篇关于使用 xslt 比较两个 xml 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文