用 xslt 比较两个 xml 文件? [英] compare two xml files with xslt?
本文介绍了用 xslt 比较两个 xml 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 2 个 xml 文件.如何使用 xslt 比较两个文件是否相等?如果不相等意味着在第二个 xml 中发生了哪些更改?
I have 2 xml files. How can I compare both files are equal or not by using xslt? If not equal means where the changes havebeen occur in the second xml?
推荐答案
在 XPath 2.0 中你可以简单地使用 fn:deep-equal
.
In XPath 2.0 you could simple use fn:deep-equal
.
遵循 XSLT 1.0 中的相同模式,此样式表:
Following the same pattern in XSLT 1.0, this stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="pSource2" select="'emp2.xml'"/>
<xsl:template match="/*">
<xsl:variable name="vDeep-equal">
<xsl:apply-templates select="." mode="deep-equal">
<xsl:with-param name="pTarget" select="document($pSource2)/*"/>
</xsl:apply-templates>
</xsl:variable>
<xsl:choose>
<xsl:when test="normalize-space($vDeep-equal)">
<xsl:text>Documents are different</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>Documents are deep equal</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="*" mode="deep-equal">
<xsl:param name="pTarget"/>
<xsl:choose>
<xsl:when test="$pTarget/self::* and
local-name()=local-name($pTarget) and
namespace-uri()=namespace-uri($pTarget) and
count(@*)=count($pTarget/@*) and
count(*|text()[normalize-space()]) =
count($pTarget/*|
$pTarget/text()[normalize-space()])">
<xsl:for-each select="@*">
<xsl:if test="$pTarget/@*[name()=name(current())] != .">
<xsl:text>false</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:for-each select="*|text()[normalize-space()]">
<xsl:variable name="vPosition" select="position()"/>
<xsl:apply-templates select="." mode="deep-equal">
<xsl:with-param name="pTarget"
select="($pTarget/*|
$pTarget/text()
[normalize-space()])
[$vPosition]"/>
</xsl:apply-templates>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>false</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="text()" mode="deep-equal">
<xsl:param name="pTarget"/>
<xsl:if test="not($pTarget/self::text() and
string() = string($pTarget))">
<xsl:text>false</xsl:text>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
输出:
Documents are different
这篇关于用 xslt 比较两个 xml 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文