help with recursive whitespace filter in

Rustom Mody rustompmody at gmail.com
Sun May 10 12:10:11 EDT 2009


I am trying to write a recursive filter to remove whitespace-only
nodes for minidom.
The code is below.

Strangely it deletes some whitespace nodes and leaves some.
If I keep calling it -- like so: fws(fws(fws(doc)))  then at some
stage all the ws nodes disappear

Does anybody have a clue?


from xml.dom.minidom import parse

#The input to fws is the output of parse("something.xml")


def fws(ele):
    """ filter white space (recursive)"""

   for c in ele.childNodes:
        if isWsNode(c):
            ele.removeChild(c)
            #c.unlink() Makes no diff whether this is there or not
        elif c.nodeType == ele.ELEMENT_NODE:
            fws(c)


def isWsNode(ele):
    return (ele.nodeType == ele.TEXT_NODE and not ele.data.strip())



More information about the Python-list mailing list