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

Uche Ogbuji uche.ogbuji@fourthought.com
Tue, 18 Jun 2002 15:25:06 -0600


> 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.
> 
> The situation is this.  I am using a namespace in a document so that I can embed cross-references in the document.  I want to be able to use the same cross-references in several document types.  With this in mind, I've also created a Python class that can read those cross-references and then export them again in several different formats, including XHtml.
> 
> The problem, though, is that this cross-reference class does not know what the parent document will be.
> 
> Normally, when I need to create a node, I use code that looks like this:
> 
> from xml.dom import DOMImplementation
> 
> def toHtml (self):
> 	dom = DOMImplementation.DOMImplementation()
> 	doc = dom.createDocument(None, u'Target', None)
> 	target = doc.createElement(u'a')
> 
> If I do this, though, I can't attach "target" to anything other than the document named "Target".  So, I was thinking of doing this:
> 
> 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.
> 
> How should I really be doing this?  Essentially, I'm trying to make an element that I can attach to any document.


A ha.  You have come up against one of the things that makes DOM such a pain.  
I'm not sure what the whole fanatical dopcument ownership pattern came from.  
Whether it was limitations on Java (but JDOM doesn't have this limitation, 
IIRC) or some obscure use case the WG was trying to address, but it is an 
eternal pain.

I think all Python  DOM implementations except for 4DOM and cDomlette allow 
you to set the ownerDocument, and I encourage you to just do so directly.  
However, I think it's unsafe for you to use the abstract node interface.  Why 
not just stick to minidom Node objects?

As a more general note: is it time to shrug off this handicap and extend 
Python DOM implementations to allow setting ownerDocument?

One downside is that there are some implementation tricks based on the tight 
relationship with ownerDocument, but I think all of these can be solved in 
other ways.

We would also wan to set proper reference and lifecycle management guidelines 
for nodes.


-- 
Uche Ogbuji                                    Fourthought, Inc.
http://uche.ogbuji.net    http://4Suite.org    http://fourthought.com
Track chair, XML/Web Services One (San Jose, Boston): 
http://www.xmlconference.com/
DAML Reference - http://www.xml.com/pub/a/2002/05/01/damlref.html
The Languages of the Semantic Web - http://www.newarchitectmag.com/documents/s=
2453/new1020218556549/index.html
XML, The Model Driven Architecture, and RDF @ XML Europe - 
http://www.xmleurope.com/2002/kttrack.asp#themodel