我有 2 个 xml 文件。如何使用 xslt 比较两个文件是否相等?如果不等于意味着第二个 xml 中发生了更改?
在 XPath 2.0 中你可以简单地使用fn:deep-equal http://www.w3.org/TR/2005/CR-xpath-functions-20051103/#func-deep-equal.
遵循 XSLT 1.0 中的相同模式,此样式表:
<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>
Output:
Documents are different
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)