[XML-SIG] Equivalence test for DOM Nodes?

Mike Brown mike at skew.org
Tue Nov 25 18:49:51 EST 2003

Roman Kennke wrote:
> Hi list,
> Is there a utility function or something to recursivly compare two DOM
> Nodes for equivalence? For instance:
> <x:a xmlns:a='http://abc.org'>
>   <y:b xmlns:b='http://xyz.org'>
>     foo
>   </y:b>
> </x:a>

I think you meant to bind 'x' and 'y' prefixes there, not 'a' and 'b'.
If you didn't, the documents definitely aren't equivalent :)

> <s:a xmlns:s='http://abc.org'>
>   <t:b xmlns:t='http://xyz.org'>
>     foo
>   </t:b>
> </s:a>

Try the Ft.Xml.Lib.TreeCompare module in 4Suite. We use it in our test suites.
I have just added doc strings to it. It is namespace aware and works with
minidom as well as Domlette.

As of today, I made it support optionally ignoring namespace declarations, so
it will work with your example. Either use current 4Suite from CVS, or it
should be safe to just plop the new TreeCompare.py into your distribution if
you've already got one of the 4Suite 1.0 alphas installed.

s1 = """<x:a xmlns:x='http://abc.org'>
  <y:b xmlns:y='http://xyz.org'>

s2 = """<s:a xmlns:s='http://abc.org'>
  <t:b xmlns:t='http://xyz.org'>

from Ft.Xml.Lib.TreeCompare import NodeCompare, TreeCompare

# use Domlette, which TreeCompare uses by default
if not TreeCompare(s1, s2, ignoreNsDecls=1):
    print "they are equal!"

# use minidom, so bypass TreeCompare and use NodeCompare directly
from xml.dom.minidom import parseString
mdoc1 = parseString(s1)
mdoc2 = parseString(s2)
if NodeCompare(mdoc1, mdoc2, ignoreNsDecls=1):
    print "they are equal!"


More information about the XML-SIG mailing list