[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
    normalize(a)
    normalize(b)
    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 />
</env:Envelope>
""")

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

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

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

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:

    http://effbot.org/downloads#elementtree

</F>






More information about the XML-SIG mailing list