473,320 Members | 1,910 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 1796
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Steven Bethard | last post by:
In the "empty classes as c structs?" thread, we've been talking in some detail about my proposed "generic objects" PEP. Based on a number of suggestions, I'm thinking more and more that instead of...
24
by: Marcin Vorbrodt | last post by:
Here is an example of my code: //Header file #include <vector> using std::vector; namespace Revelation { // class definitions, etc... // class members are of type std::vector }
2
by: Mike Morse | last post by:
What see sample that show xs:element where the xs namespace = http://www.w3.org/2001/XMLSchema However, I see another example with xsi: where xsi = http://www.w3.org/2001/XMLSchema-instance ...
3
by: Jim Heavey | last post by:
Trying to get the hang of Namespaces. I have primarly developed in VB and am transitioning to C# and the .Net Environment. I have worked a bit with Java as well in school about a year or so ago....
17
by: clintonG | last post by:
Using 2.0 with Master Pages and a GlobalBaseClass for the content pages. I understand the easy part -- the hierarchical structure of a namespace naming convention -- but the 2.0 IDE does not...
11
by: Random | last post by:
I'm confused about the proper use and usefulness of namespaces. I beleive I understand the purpose is so the developer can put classes within namespaces to essentially organize your code. And I...
0
by: News Reader | last post by:
A year or so ago, there was a posting - I believe on someone's blog - which told of a unique experiment. The experimenter tried loading and searching a large XML document based on three...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.