Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ElemenTree and namespaces

509 views
Skip to first unread message

Matthew Thorley

unread,
May 16, 2005, 2:03:08 PM5/16/05
to
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.

thanks

Andrew Dalke

unread,
May 16, 2005, 3:48:18 PM5/16/05
to
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?

See http://online.effbot.org/2004_08_01_archive.htm#20040803
(starting with "more xml"). That was a response to Uche's article at
http://www.xml.com/pub/a/2004/06/30/py-xml.html
and with a followup at
http://www.xml.com/pub/a/2004/08/11/py-xml.html

Andrew
da...@dalkescientific.com

oren....@gmail.com

unread,
May 16, 2005, 3:49:17 PM5/16/05
to

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

Matthew Thorley

unread,
May 16, 2005, 5:00:41 PM5/16/05
to
Thanks Andrew & Oren, that should do the trick.

Fredrik Lundh

unread,
May 17, 2005, 2:38:14 AM5/17/05
to pytho...@python.org
oren....@gmail.com wrote:

> 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.

if your XPath processor requires you to use the same namespace
prefixes in the XPath expression as you're using in the document,
that processor is broken.

(XPath processors that can only work on expressions embedded
in XML documents may be an exception to that rule; they can get
the prefix/URI mapping from the execution context)

</F>

0 new messages