Re: "Empty" Text Nodes

> On the other hand it's no big effort transforming a DOM tree with
> whitespace text nodes into another one without them ...

Correct.  In fact, 4DOM comes with a simple helper function: DOM.Ext.Strip, 
which does this using the Level 2 NodeIterators.  Here's the function, if 
anyone is interested.  It's in Python, but it uses the strict DOM API and so 
should be quite readable to a non-Pythoneer:

def Strip(start_node, skip_elements=["PRE", "STYLE", "SCRIPT"]):
    remove_list = []
    snit = start_node.createSelectiveTreeIterator(DOM.DOMIF.Node.TW_TEXT)
    curr_node = snit.nextNode()
    while curr_node:
        #first of all make sure it is not inside one of the skip_elements
        ancestor = curr_node
        while ancestor != start_node:
            if ancestor.getNodeType() == DOM.DOMIF.Node.ELEMENT_NODE:
                if ancestor.getNodeName() in skip_elements:
                    break
            ancestor = ancestor.getParentNode()
        else:
            curr_node.setData(string.strip(curr_node.getData()))
            if not len(curr_node.getData()):
                remove_list.append(curr_node)
        curr_node = snit.nextNode()
    for node_to_remove in remove_list:
        node_to_remove.getParentNode().removeChild(node_to_remove)
    return start_node


-- 
Uche Ogbuji
FourThought LLC, IT Consultants
uche.ogbuji@fourthought.com	(970)481-0805
Software engineering, project management, Intranets and Extranets
http://FourThought.com		http://OpenTechnology.org

Received on Saturday, 27 February 1999 00:32:42 UTC