ElemenTree and namespaces

oren.tirosh at gmail.com oren.tirosh at gmail.com
Mon May 16 15:49:17 EDT 2005


Matthew Thorley wrote:
> Does any one know if there a way to force the ElementTree module to
> print out name spaces 'correctly' rather than as ns0, ns1 etc? Or is
> there at least away to force it to include the correct name spaces in
> the output of tostring?
>
> I didn't see anything in the api docs or the list archive, but before
I
> set off to do it myself I thought I should ask, because it seemed
like
> the kind of thing that has already been done.

There's a way, but it requires access to an undocumented internal
stuff. It may not be compatible with other implementations of the
ElementTree API like lxml.

The ElementTree module has a _namespace_map dictionary of "well known"
namespace prefixes mapping namespace URIs to prefixes. By default it
contains the xml:, html:, rdf: and wsdl:. You can add your own
namespace to that dictionary to get your preferred prefix.

In theory, namespace prefixes are entirely arbitrary and only serve as
a temporary link to the namespace URI. In practice, people tend to get
emotionally attached to their favorite prefixes. XPath also breaks this
theory because it refers to prefixes rather than URIs.

<plug>
Take a look at http://www.tothink.com/python/ElementBuilder. It's a
module to provide a friendly syntax for building and populating
Elements:

Example:

>>> import ElementBuilder
>>> from elementtree import ElementTree
>>> ns = ElementBuilder.Namespace('http://some.uri', 'ns')
>>> e = ns.tag(
...   ns.tag2('content'),
...   ns.tag3(attr='value'),
...   ns.tag4({ns.attr: 'othervalue'}),
...   ns.x(
...     ns.y('y'),
...     ns.z('z'),
...     'some text',
...   )
... )
>>> ElementTree.dump(e)
<ns:tag xmlns:ns="http://some.uri"><ns:tag2>content</ns:tag2><ns:tag3
attr="value" /><ns:tag4 ns:attr="othervalue"
/><ns:x><ns:y>y</ns:y><ns:z>z</ns:z>some text</ns:x></ns:tag>

Note that the namespace prefix on output is not "ns0". The second
argument to the Namespace constructor is the prefix hint and unless it
collides with any other namespace or prefix it will be added to
_namespace_map dictionary and used on output.
</plug>

  Oren




More information about the Python-list mailing list