比较两个xml文件与xslt? [英] compare two xml files with xslt?
本文介绍了比较两个xml文件与xslt?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有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中, http://www.w3.org/TR/2005/CR-xpath-functions-20051103/#func-deep-equal =nofollow> 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
这篇关于比较两个xml文件与xslt?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文