[XML-SIG] Re: Equivalence test for DOM Nodes?

Fredrik Lundh fredrik at pythonware.com
Tue Nov 25 15:55:27 EST 2003

Roman Kennke wrote:

> I need a more relaxed interpretation of equal, where the prefix is not
> considered. In fact, I need this kind of comparison for the SOAP 1.2
> testsuite. I think, it does not matter if an element is named <env:Envelope>
> or <soap:Envelope> as long as env and soap refer to the same
> namespaceURI.

fwiw, the following ElementTree snippet does what you want:

from elementtree.ElementTree import XML, tostring

def normalize(a):
    # get rid of whitespace
    for i in a.getiterator():
        if i.text:
            i.text = i.text.strip()
        i.tail = None

def compare(a, b):
    # normalize and compare serializations
    return tostring(a) == tostring(b)

# tree ways to describe the same thing

a = XML("""\
<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'>
    <env:Body />

b = XML("""\
<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>

c = XML("""\
<Envelope xmlns='http://schemas.xmlsoap.org/soap/envelope/'><Body/></Envelope>

>>> compare(a, b)
>>> compare(a, c)
>>> compare(b, c)

you may want to make the "normalize" function a bit more selective
(e.g. by preserving whitespace inside soap:Body subelements, etc)

for downloads and more info, see:



More information about the XML-SIG mailing list