[XML-SIG] Proper way of generating a parentless Node object

Martin v. Loewis martin@v.loewis.de
18 Jun 2002 22:55:12 +0200

Mark Humphrey <msph@alyra.org> writes:

> Okay, this is a kind of odd situation, but I need to know the
> correct way to create a Node (Text node or Element, I actually have
> to create both) that is not attached to a particular document and
> that can be inserted into any given document.

That is not possible. You need atleast to guarantee that the node is
inserted into a Document originating from the same DOMImplementation.

> from xml.dom import Node
> def toHtml (self):
> 	target = Node()
> 	target.nodeName = u'a'
> 	target.nodeType = Node.ELEMENT_NODE
> 	return target
> But that has me accessing not only internal object variables, it has
> me *setting* variables that the DOM spec says are supposed to be
> read-only.

Furthermore, your instances are of the wrong class: "good" nodes are
instances of subclasses of Node.

> How should I really be doing this?

I believe what you want to do is not possible with standard DOM
API. So you need to revert to implementation-specific extensions. For
4DOM, you can use _4dom_setOwnerElement.