def remove_whilespace_nodes(node, unlink=False): """Removes all of the whitespace-only text decendants of a DOM node. When creating a DOM from an XML source, XML parsers are required to consider several conditions when deciding whether to include whitespace-only text nodes. This function ignores all of those conditions and removes all whitespace-only text decendants of the specified node. If the unlink flag is specified, the removed text nodes are unlinked so that their storage can be reclaimed. If the specified node is a whitespace-only text node then it is left unmodified.""" remove_list = [] for child in node.childNodes: if child.nodeType == dom.Node.TEXT_NODE and \ not child.data.strip(): remove_list.append(child) elif child.hasChildNodes(): remove_whilespace_nodes(child, unlink) for node in remove_list: node.parentNode.removeChild(node) if unlink: node.unlink()