Re: [xsl] Testing 2 XML documents for equality - a solution

Subject: Re: [xsl] Testing 2 XML documents for equality - a solution
From: David Carlisle <davidc@xxxxxxxxx>
Date: Mon, 4 Apr 2005 14:53:03 +0100
>> What is wrong with the much simpler alternative of just 
>> writing out the string corresponding to a specific 
>> "canonical" linearisation, and then just comparing those two strings?

>I guess because how do you map back to the element/attribute that
>doesn't match once you've done the comparison.

Not sure what you mean here.

If I wanted to compare two documents in xslt1 I would probably just do


<xsl:template name="x">
  <xsl:apply-templates mode="verb" select="$doc1"/>
</xsl:template>

<xsl:template name="y">
  <xsl:apply-templates mode="verb" select="$doc2"/>
</xsl:template>

<xsl:text>doc1 = doc2 is: </xsl:text>
<xsl:value-of select="string($x) = string($y)"/>

where mode verb  just linearises stuff, eg:

<xsl:template match="*" mode="verb">
  <xsl:text>&lt;</xsl:text>
  <xsl:value-of select="local-name()"/>
  <xsl:text> xmlns="</xsl:text>
  <xsl:value-of select="namespace-uri()"/>
  <xsl:for-each select="*">
  <xsl:sort select="name()"/>
  ....
  </xsl:for-each>
  <xsl:text>&gt;</xsl:text>
  <xsl:apply-templates mode="verb"/>
  <xsl:text>&lt;/</xsl:text>
  <xsl:value-of select="local-name()"/>
  <xsl:text>&gt;</xsl:text>
</xsl:template>

with other templates (or not) for PIs and comments, according to taste.

Oh I suppose you mean if it fails how do you know _where_ it fails

That's a rather different problem than comparison and if that is the
problem I wouldn't use this kind of technique at all, rather just walk
over the two trees one node at a time, comparing as you go and stop as
soon as you find anything bad. As Michael commented at the start of the
thread that's likely to have much better performance anyway if you
expect teh documents to be non-equal, eg it will stop as soon as it
finds that the top level elements differ, whereas a
canonicalise-and-compare approach will always generate the entire string
for both documents.

David


________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

Current Thread