Default namespace and attributes with ElementTree 1.3

Racinet Georges georges at
Thu Jun 18 07:21:56 EDT 2009

Hi there,

I've got a problem with the way ElementTree 1.3a3-20070912 handles  
with the default namespace feature (please feel free to redirect me if  
this isn't the
proper place to discuss such matters).

 >>> from elementtree import ElementTree as ET
 >>> e = ET.fromstring('<a xmlns="foo"><b id="1"/></a>')
 >>> ET.tostring(e)
'<ns0:a xmlns:ns0="foo"><ns0:b id="1" /></ns0:a>'

Notice that the 'id' attribute does not belong to the "foo" namespace.
If I got the XML 1.0 specification right, this is perfectly normal
(Quoting :

"Default namespace declarations do not apply directly to attribute  
the interpretation of unprefixed attributes is determined by the  
element on
which they appear.
The namespace name for an unprefixed attribute name always has no  

Now, trying to serialize this back with the default_namespace feature  
of ET 1.3,
I get an error, as predicted by the online documentation

 >>> t = ET.ElementTree(e)
 >>> t.write(sys.stdout, default_namespace='foo')
Traceback (...)
ValueError: cannot use non-qualified names with default_namespace option

Since this is a parse/serialize round-trip, I think that this  
behaviour isn't right, and that
unprefixed attributes should go through. Actually, shouldn't  
attributes even be outside
of the scope of the default namespace feature ?

Is there hope for future improvements about this  ?
I saw some FIXME comment in the source. The svn head version (rev 528)
seems to be identical in that respect, by the way.

Removing in the part where the ValueError is been raised
seems to solve the issue at first sight :
 >>> t.write(sys.stdout, default_namespace='foo')
<a xmlns="foo"><b id="1" /></a>

but this lets also unprefixed elements go through, which is unwanted:
 >>> e = ET.fromstring('<a><b id="1"/></a>')
 >>> t = ET.ElementTree(e)
 >>> t.write(sys.stdout, default_namespace='foo')
<a xmlns="foo"><b id="1" /></a>

I wouldn't care in my application, whose output is xhtml, though.

Georges Racinet,
Zope/CPS/Plone expertise, assistance & development
GPG: 0x4862FFF7

-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 489 bytes
Desc: This is a digitally signed message part
URL: <>

More information about the Python-list mailing list