help with recursive whitespace filter in

Steve Howell showell30 at yahoo.com
Sun May 10 18:49:46 CEST 2009


On May 10, 9:10 am, Rustom Mody <rustompm... at gmail.com> wrote:
> 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.



More information about the Python-list mailing list