[XML-SIG] comparing xml documents

Mark McEahern marklists@mceahern.com
Wed, 29 Aug 2001 18:19:19 -0700


Suppose I have two xml documents:

	>>> s1 = "<Foo id='1' type='bar'/>"
	>>> s2 = "<Foo type='bar' id='1'/>"

and I want to make sure they are equal.

This, clearly, won't do:

	>>> s1==s2
	0

So I think, gee, lemme load 'em into a DOM:

	>>> from xml.dom.minidom import parseString
	>>> d1 = parseString(s1)
	>>> d2 = parseString(s2)

Of course, I can't compare the two documents simply:

	>>> d1==d2
	0

But I can do this:

	>>> d1.toxml()==d2.toxml()
	1

Unfortunately, that's not all she wrote...

For slightly more complicated cases, this simple approach doesn't work:

	>>> s1 = "<Foo><Bar/><Bar2/></Foo>"
	>>> s2 = "<Foo><Bar2/><Bar/></Foo>"
	>>> d1 = parseString(s1)
	>>> d2 = parseString(s2)
	>>> d1.toxml()==d2.toxml()
	0

Of course, this example assumes that the order of siblings is irrelevant for
comparison purposes--are there apps that are dependent on the order of
sibling nodes?  Yikes.

It should be fairly easy to implement a helper comparison function for the
DOM (you could even give it hints like ignore sibling order or strict
sibling order), but since I'm eminently lazy, I figured I'd ask you folks if
someone has already made that relatively modest effort unnecessary.

Thanks,

// mark

p.s.  Oh the philosophy classes I could have skipped if only I had known
that truth was nonzero!  (That's a lame joke, isn't it?)