[XML-SIG] Why is Builder.push() the way it is?

Michael Sanborn michael@graphion.com
Thu, 03 Dec 1998 10:25:21 -0800


I'm new to Python, and am very interested in using the DOM
implementation. So I'm puzzling over something in builder.py, and hope
to have help understanding it.

There's a section in push() like this:

if self.current_element:
    self.current_element.insertBefore(node, None)
elif nodetype in _LEGAL_DOCUMENT_CHILDREN:
    if nodetype == TEXT_NODE:
        if string.strip(node.get_nodeValue()) != "":
        self.document.appendChild(node)
    else:
        self.document.appendChild(node)

Now, as far as I can see from the DOM spec and the definition of
_LEGAL_DOCUMENT_CHILDREN, if nodetype is in _LEGAL_DOCUMENT_CHILDREN,
nodetype will never be equal to TEXT_NODE. I was rather imagining that
this section would read:

if self.current_element:
    if nodetype == TEXT_NODE:
        if string.strip(node.get_nodeValue()) != "":
            self.document.appendChild(node)
    else:
        self.current_element.insertBefore(node, None)
elif nodetype in _LEGAL_DOCUMENT_CHILDREN:
    self.document.appendChild(node)

I expect that I'm mistaken, but I'd like to know why.

As a second, minor question, why does one sometimes use
appendChild(node) and other times insertBefore(node, None)?

Best regards,

Michael Sanborn
Graphion Typesetting