XML and namespaces

Alan Kennedy alanmk at hotmail.com
Tue Dec 6 00:29:17 CET 2005


[Fredrik Lundh]
>>can anyone perhaps dig up a DOM L2 implementation that's not written
>>by anyone involved in this thread, and see what it does ?

[Paul Boddie]
 > document = libxml2dom.createDocument(None, "doc", None)
 > top = document.xpath("*")[0]
 > element = document.createElementNS("DAV:", "href")
 > document.replaceChild(element, top)
 > print document.toString()
 >
 > This outputs the following:
 >
 > <?xml version="1.0"?>
 > <href xmlns="DAV:"/>

But that's incorrect. You have now defaulted the namespace to "DAV:" for 
every unprefixed element that is a descendant of the href element.

Here is an example

#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
document = libxml2dom.createDocument(None, "doc", None)
top = document.xpath("*")[0]
elem1 = document.createElementNS("DAV:", "href")
document.replaceChild(elem1, top)
elem2 = document.createElementNS(None, "no_ns")
document.childNodes[0].appendChild(elem2)
print document.toString()
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

which produces

"""
<?xml version="1.0"?>
<href xmlns="DAV:"><no_ns/></href>
"""

The defaulting rules of XML namespaces state

"""
5.2 Namespace Defaulting

A default namespace is considered to apply to the element where it is 
declared (if that element has no namespace prefix), and to all elements 
with no prefix within the content of that element.
"""

http://www.w3.org/TR/REC-xml-names/#defaulting

So although I have explicitly specified no namespace for the no_ns 
subelement, it now defaults to the default "DAV:" namespace which has 
been declared in the automagically created xmlns attribute. This is 
wrong behaviour.

If I want for my sub-element to truly have no namespace, I have to write 
it like this

"""
<?xml version="1.0"?>
<myns:href xmlns:myns="DAV:"><no_ns/></myns:href>
"""

[Paul Boddie]
 > Leaving such
 > attributes out by default, whilst claiming some kind of "fine print"
 > standards compliance, is really a recipe for unnecessary user
 > frustration.

On the contrary, once you start second guessing the standards and making 
guesses about what users are really trying to do, and making decisions 
for them, then some people are going to get different behaviour from 
what they rightfully expect according to the standard. People whose 
expectations match with the guesses made on their behalf will find that 
their software is not portable between DOM implementations.

With something as finicky as XML namespaces, you can't just make ad-hoc 
decisions as to what the user "really wants". That's why DOM L2 punted 
on the whole problem, and left it to DOM L3.

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



More information about the Python-list mailing list