XML and namespaces

Alan Kennedy alanmk at hotmail.com
Sat Dec 3 09:30:01 EST 2005


[uche.ogbuji at gmail.com]
>>Oh no.  That only means that namespace declaration attributes are not
>>created in the DOM data structure.  However, output has to fix up
>>namespaces in .namespaceURI properties as well as directly asserted
>>"xmlns" attributes.  It would be silly for DOM to produce malformed
>>XML+XMLNS, and of course it is not meant to.  The minidom behavior
>>needs fixing, badly.

[Alan Kennedy]
> My interpretation of namespace nodes is that the application is
> responsible for creating whatever namespace declaration attribute nodes
> are required, on the DOM tree.
> 
> DOM should not have to imply any attributes on output.

[uche.ogbuji at gmail.com]
>  ..... you have to perform fix-up to merge properties with
> explicit NSDEcl attributes in order to serialize.  If it does not do
> so, it is losing all the information in namespace properties, and the
> resulting output is not the same document that is represented in the
> DOM.

Well, my reading of the DOM L2 spec is such that it does not agree with 
the statement above.

http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html

Section 1.1.8: XML Namespaces

"""
Namespace validation is not enforced; the DOM application is 
responsible. In particular, since the mapping between prefixes and 
namespace URIs is not enforced, in general, the resulting document 
cannot be serialized naively. For example, applications may have to 
declare every namespace in use when serializing a document.
"""

To me, this means that, as AMK originally stated, that DOM L2 is capable 
of creating documents that are not well-formed wrt to namespaces, i.e. 
"cannot be serialized naively". It is the application authors 
responsibility to ensure that their document is well formed.

Also, there is the following important note

"""
Note: In the DOM, all namespace declaration attributes are by definition 
bound to the namespace URI: "http://www.w3.org/2000/xmlns/". These are 
the attributes whose namespace prefix or qualified name is "xmlns".
"""

These namespace declaration nodes, i.e. attribute nodes in the 
xml.dom.XMLNS_NAMESPACE namespace, are a pre-requisite for any 
namespaced DOM document to be well-formed, and thus naively serializable.

The argument could be made that application authors should be protected 
from themselves by having the underlying DOM library automatically 
create the relevant namespace nodes.

But to me that's not pythonic: it's implicit, not explicit.

My vote is that the existing xml.dom.minidom behaviour wrt namespace 
nodes is correct and should not be changed.

regards,

-- 
alan kennedy
------------------------------------------------------
email alan:              http://xhaus.com/contact/alan



More information about the Python-list mailing list