XML and namespaces

Paul Boddie paul at boddie.org.uk
Mon Dec 5 17:01:35 EST 2005


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

Alright. Look away from the wrapper code (which I wrote, and which
doesn't do anything particularly clever) and look at the underlying
libxml2 serialisation behaviour:

import libxml2dom
document = libxml2dom.createDocument("DAV:", "href", None)
print document.toString()

This outputs the following:

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

To reproduce the creation of bare Document objects (which I thought
wasn't strictly supported by minidom), we perform some tricks:

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:"/>

While I can understand the desire to suppress xmlns attribute
generation for certain document types, this is probably only
interesting for legacy XML processors and for HTML. Leaving such
attributes out by default, whilst claiming some kind of "fine print"
standards compliance, is really a recipe for unnecessary user
frustration.

Paul




More information about the Python-list mailing list