Skip to Content.
Sympa Menu

xom-interest - Re: [XOM-interest] Comparing two semantically equal XML files

xom-interest AT

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: Elliotte Harold <elharo AT>
  • To: Marcelo Schneider <marcelo.schneider AT>
  • Cc: xom-interest AT
  • Subject: Re: [XOM-interest] Comparing two semantically equal XML files
  • Date: Fri, 10 Oct 2008 06:10:25 -0700

Marcelo Schneider wrote:

What I need to do is, when a user opens an already existing graph and tries to save it, compare the new XML sent with the previously existing XML in the server, saving only if they are different (so changing dates are correct).

Googling around for a while I found out that a possible way to do that without worrying with XML structure was to use Canonicalization, and that XOM could possibly help me with that. Is this assumption correct? Here is what I'm currently doing:

With caveats. XOM supports XML canonicalization, but canonicalization may not be what you need. For many applications, the definition of "equivalence" is very application specific.

However, the above code returns false when I compare XMLs like these (they all start with "<?xml version="1.0" encoding="UTF-8"?>"):

xml1: "<tag1><tag2 id="1" name="a">Some Text</tag2></tag1><other tag />"
xml2: "<tag1>\n\t<tag2 id="1" name="a">Some Text</tag2>\r</tag1><other tag />" (with new lines, tabs and carriage returns)

The line feeds and tabs between your tags are significant to XML canonicalization and therefore these documents are not the same according to that definition.

xml3: "<tag1><tag2 id="1" name="a">Some Text</tag2></tag1><other tag></other tag>" (other valid xml representation)

Order is also significant.

xml4: "<tag1><tag2 name="a" id="1">Some Text</tag2></tag1><other tag />" (attributes order reversed)

Attribute order is not significant. xml4 is the same as xml3 (unless I'm missing simething) but not the same as xml1 or xml2.

xml5: "<other tag /><tag1><tag2 id="1" name="a">Some Text</tag2></tag1>" (yet another thing that looks the same, but I'm probably wrong assuming this should count as equal)

Very different. The bottom line is that you may well need to define and implement your own definition of equivalence. There's no one standard definition that works for all or even a plurality of cases.

Elliotte Rusty Harold elharo AT
Refactoring HTML Just Published!

Archive powered by MHonArc 2.6.24.

Top of Page