[XML-SIG] normalize() for minidom

Stephan Tolksdorf andorxor@gmx.de
Fri, 20 Oct 2000 14:06:51 +0200


I'd like to have the normalize() method of DOM2's node interface
(DOM1's normalize() was in the element interface) in minidom

You will find my try of an implementation at the end of this mail.

--- documentation ---
This is the description from the candidate recommendation of dom2:

normalize (introduced in DOM Level 2)
Puts all Text nodes in the full depth of the sub-tree underneath this
Node, including attribute nodes, into a "normal" form where only
structure (e.g., elements, comments, processing instructions, CDATA
sections, and entity references) separates Text nodes, i.e., there
are neither adjacent Text nodes nor empty Text nodes. This can be
used to ensure that the DOM view of a document is the same as if
it were saved and re-loaded, and is useful when operations (such
as XPointer lookups) that depend on a particular document tree
structure are to be used. Note: In cases where the document
contains CDATASections, the normalize operation alone may not be
sufficient, since XPointers do not differentiate between Text
nodes and CDATASection nodes.

No Parameters
No Return Value
No Exceptions

--- implementation ---
class Node:
    def normalize(self):
        """Joins adjacent Text nodes and deletes empty Text nodes
        in the full depth of the sub-tree underneath this Node.
        i = 0
        while i < len(self.childNodes):
            cn = self.childNodes[i]
            if cn.nodeType == Node.TEXT_NODE:
                i += 1
                # join adjecent Text nodes
                while i < len(self.childNodes) and self.childNodes[i].nodeType == Node.TEXT_NODE:
                     cn.nodeValue = cn.data = cn.data + self.childNodes[i].data
                # delete empty nodes      
                if cn.nodeValue == "":
                    i -= 1             
            elif cn.nodeType == Node.ELEMENT_NODE: cn.normalize()
            i += 1      

Best regards,
  Stephan Tolksdorf