help with recursive whitespace filter in

R

Rustom Mody

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())
 
S

Steve Howell

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())

I would avoid doing things like delete/remove in a loop. Instead
build a list of things to delete.
 
R

rustom

I would avoid doing things like delete/remove in a loop.  Instead
build a list of things to delete.

Yeah I know. I would write the whole damn thing functionally if I knew
how. But cant figure out the API.
I actually started out to write a (haskell-style) copy out the whole
tree minus the unwanted nodes but could not figure it out
 
M

MRAB

rustom said:
Yeah I know. I would write the whole damn thing functionally if I knew
how. But cant figure out the API.
I actually started out to write a (haskell-style) copy out the whole
tree minus the unwanted nodes but could not figure it out
def fws(ele):
""" filter white space (recursive)"""
empty_nodes = []
for c in ele.childNodes:
if isWsNode(c):
empty_nodes.append(c)
elif c.nodeType == ele.ELEMENT_NODE:
fws(c)
for c in empty_nodes:
ele.removeChild(c)
 
S

Steve Howell

Yeah I know. I would write the whole damn thing functionally if I knew
how.  But cant figure out the API.
I actually started out to write a (haskell-style) copy out the whole
tree minus the unwanted nodes but could not figure it out

You can use list comprehensions for a more functional style.

Instead of deleting the unwanted nodes in place, try to create new
lists of just the wanted results.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top