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