470,826 Members | 1,941 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,826 developers. It's quick & easy.

ElemenTree and namespaces

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
Jul 19 '05 #1
4 1623
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_...e.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

Jul 19 '05 #2
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

Jul 19 '05 #3
Thanks Andrew & Oren, that should do the trick.
Jul 19 '05 #4
or*********@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>

Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

24 posts views Thread by Marcin Vorbrodt | last post: by
2 posts views Thread by Mike Morse | last post: by
3 posts views Thread by Jim Heavey | last post: by
11 posts views Thread by Random | last post: by
reply views Thread by News Reader | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.