[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Testing 2 XML documents for equality - a solution

Subject: Re: Testing 2 XML documents for equality - a solution
From: David Carlisle <davidc@xxxxxxxxx>
Date: Mon, 4 Apr 2005 14:53:03 +0100
compare two strings in xsl
>> 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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.