ElementTree Namespace Prefixes

Chris Spencer usenet.20.evilspam at spamgourmet.com
Sun Jun 12 19:55:00 CEST 2005


Andrew Dalke wrote:
> On Sun, 12 Jun 2005 15:06:18 +0000, Chris Spencer wrote:
> 
> 
>>Does anyone know how to make ElementTree preserve namespace prefixes in 
>>parsed xml files?
> 
> 
> See the recent c.l.python thread titled "ElemenTree and namespaces"
> and started "May 16 2:03pm".  One archive is at
> 
> http://groups-beta.google.com/group/comp.lang.python/browse_thread/thread/31b2e9f4a8f7338c/363f46513fb8de04?&rnum=3&hl=en

Thanks, although that thread didn't seem to resolve the issue. All the 
first few links talk about is how to hack your own parser to make sense 
of the Clark notation.

The problem at hand is with how Elementtree outputs namespaces and 
represents the tag name in memory.

Given xml with no namespaces, Elementtree works perfectly. However, if 
you give the root tag an xmlns attribute, Elementtree relabels all child 
nodes with it's own prefix, completely defeating the purpose of the 
default namespace. In my opinion, this is unacceptable behavior.

If an XML parser reads in and then writes out a document without having 
altered it, then the new document should be the same as the original. 
With Elementtree this isn't so. Lundh apparently believes he knows 
better than you and I on how our namespaces should be represented.

It's a shame the default ns behavior in Elementtree is in such a poort 
staten. I'm surprised no one's forked Elementtree solely to fix this issue.

Anyways, Python's native minidom works as expected, so I'll probably use 
that instead, even if the api is slightly less intuitive.

Chris



More information about the Python-list mailing list