473,395 Members | 1,386 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,395 software developers and data experts.

ElementTree Namespace Prefixes

Does anyone know how to make ElementTree preserve namespace prefixes in
parsed xml files? The default behavior is to strip a document of all
prefixes and then replace them autogenerated prefixes like ns0, ns1,
etc. The correct behavior should be to write the file in the form that
it was read, which it seems to do correctly for everything except
namespace prefixes. The docs mention "proper" output can be achieved by
using the Qname object, but they don't go into any detail. Any help is
appreciated.

Thanks,
Chris Spencer
Jul 19 '05 #1
9 10047
On Sun, 12 Jun 2005 15:06:18 +0000, Chris Spencer wrote:
Does anyone know how to make ElementTree preserve namespace prefixes in
parsed xml files?


See the recent c.l.python thread titled "ElemenTree and namespaces"
and started "May 16 2:03pm". One archive is at

http://groups-beta.google.com/group/...?&rnum=3&hl=en

Andrew
da***@dalkescientific.com

Jul 19 '05 #2
Andrew Dalke wrote:
On Sun, 12 Jun 2005 15:06:18 +0000, Chris Spencer wrote:

Does anyone know how to make ElementTree preserve namespace prefixes in
parsed xml files?

See the recent c.l.python thread titled "ElemenTree and namespaces"
and started "May 16 2:03pm". One archive is at

http://groups-beta.google.com/group/...?&rnum=3&hl=en


Thanks, although that thread didn't seem to resolve the issue. All the
first few links talk about is how to hack your own parser to make sense
of the Clark notation.

The problem at hand is with how Elementtree outputs namespaces and
represents the tag name in memory.

Given xml with no namespaces, Elementtree works perfectly. However, if
you give the root tag an xmlns attribute, Elementtree relabels all child
nodes with it's own prefix, completely defeating the purpose of the
default namespace. In my opinion, this is unacceptable behavior.

If an XML parser reads in and then writes out a document without having
altered it, then the new document should be the same as the original.
With Elementtree this isn't so. Lundh apparently believes he knows
better than you and I on how our namespaces should be represented.

It's a shame the default ns behavior in Elementtree is in such a poort
staten. I'm surprised no one's forked Elementtree solely to fix this issue.

Anyways, Python's native minidom works as expected, so I'll probably use
that instead, even if the api is slightly less intuitive.

Chris
Jul 19 '05 #3
Chris Spencer napisał(a):
Given xml with no namespaces, Elementtree works perfectly. However, if
you give the root tag an xmlns attribute, Elementtree relabels all child
nodes with it's own prefix, completely defeating the purpose of the
default namespace. In my opinion, this is unacceptable behavior.
There is no functional difference between default namespace and "normal"
namespace. Replacing "default" with "normal" has no effect for document
processing (namespace doesn't change, only prefix), although it looks
differently for humans. Anyway, XML is for machines, not for humans.
If an XML parser reads in and then writes out a document without having
altered it, then the new document should be the same as the original.
With Elementtree this isn't so. Lundh apparently believes he knows
better than you and I on how our namespaces should be represented.
No, this is perfectly valid behaviour. Go, see spec.
It's a shame the default ns behavior in Elementtree is in such a poort
staten. I'm surprised no one's forked Elementtree solely to fix this issue.


There is at least one ElementTree API implementation that retains
prefixes, lxml.ETree. Go google for it.

--
Jarek Zgoda
http://jpa.berlios.de/ | http://www.zgodowie.org/
Jul 19 '05 #4
Chris Spencer wrote:
If an XML parser reads in and then writes out a document without having
altered it, then the new document should be the same as the original.
says who?
With Elementtree this isn't so. Lundh apparently believes he knows
better than you and I on how our namespaces should be represented.


do you even understand how XML namespaces work?

</F>

Jul 19 '05 #5
Fredrik Lundh wrote:
Chris Spencer wrote:
If an XML parser reads in and then writes out a document without having
altered it, then the new document should be the same as the original.


says who?


Good question. There is no One True Answer even within the XML
standards.

It all boils down to how you define "the same". Which parts of the XML
document are meaningful content that needs to be preserved and which
ones are mere encoding variations that may be omitted from the internal
representation?

Some relevant references which may be used as guidelines:

* http://www.w3.org/TR/xml-infoset
The XML infoset defines 11 types of information items including
document type declaration, notations and other features. It does not
appear to be suitable for a lightweight API like ElementTree.

* http://www.w3.org/TR/xpath-datamodel
The XPath data model uses a subset of the XML infoset with "only" seven
node types.

http://www.w3.org/TR/xml-c14n
The canonical XML recommendation is meant to describe a process but it
also effectively defines a data model: anything preserved by the
canonicalization process is part of the model. Anything not preserved
is not part of the model.

In theory, this definition should be equivalent to the xpath data model
since canonical XML is defined in terms of the xpath data model. In
practice, the XPath data model defines properties not required for
producing canonical XML (e.g. unparsed entities associated with
document note). I like this alternative "black box" definition because
provides a simple touchstone for determining what is or isn't part of
the model.

I think it would be a good goal for ElementTree to aim for compliance
with the canonical XML data model. It's already quite close.

It's possible to use the canonical XML data model without being a
canonical XML processor but it would be nice if parse() followed by
write() actually passed the canonical XML test vectors. It's the
easiest way to demonstrate compliance conclusively.

So what changes are required to make ElementTree canonical?

1. PI nodes are already supported for output. Need an option to
preserve them on parsing
2. Comment nodes are already support for output. Need an option to
preserve them on parsing (canonical XML also defines a "no comments"
canonical form)
3. Preserve Comments and PIs outside the root element (store them as
children of the ElementTree object?)
4. Sorting of attributes by canonical order
5. Minor formatting and spacing issues in opening tags

oh, and one more thing...

6. preserve namespace prefixes ;-)
(see http://www.w3.org/TR/xml-c14n#NoNSPrefixRewriting for rationale)

Jul 19 '05 #6
Oren Tirosh wrote:
It all boils down to how you define "the same". Which parts of the XML
document are meaningful content that needs to be preserved and which
ones are mere encoding variations that may be omitted from the internal
representation?

Some relevant references which may be used as guidelines:

* http://www.w3.org/TR/xml-infoset
The XML infoset defines 11 types of information items including
document type declaration, notations and other features. It does not
appear to be suitable for a lightweight API like ElementTree.

* http://www.w3.org/TR/xpath-datamodel
The XPath data model uses a subset of the XML infoset with "only" seven
node types.

http://www.w3.org/TR/xml-c14n
The canonical XML recommendation is meant to describe a process but it
also effectively defines a data model: anything preserved by the
canonicalization process is part of the model. Anything not preserved
is not part of the model.


you forgot

http://effbot.org/zone/element-infoset.htm

which describes the 3-node XML infoset subset used by ElementTree.

</F>

Jul 19 '05 #7
> you forgot

http://effbot.org/zone/element-infoset.htm

which describes the 3-node XML infoset subset used by ElementTree.


No, I did not forget your infoset subset. I was comparing it with other
infoset subsets described in various XML specifications.

I agree 100% that prefixes were not *supposed* to be part of the
document's meaning back when the XML namespace specification was
written, but later specifications broke that.

Please take a look at http://www.w3.org/TR/xml-c14n#NoNSPrefixRewriting

"... there now exist a number of contexts in which namespace prefixes
can impart information value in an XML document..."

"...Moreover, it is possible to prove that namespace rewriting is
harmful, rather than simply ineffective."

Jul 19 '05 #8
Jarek Zgoda wrote:
[snip]
It's a shame the default ns behavior in Elementtree is in such a poort
staten. I'm surprised no one's forked Elementtree solely to fix this
issue.


There is at least one ElementTree API implementation that retains
prefixes, lxml.ETree. Go google for it.


Just to make it explicitly clear, lxml is not a fork of ElementTree
fork, but a reimplementation of the API on top of libxml2.

ElementTree indeed retains prefixes, and since version 0.7 released
earlier this way, it's also possible to get some control over generation
of prefixes during element construction.

You can find it here:

http://codespeak.net/lxml

Regards,

Martijn

Jul 19 '05 #9
Chris Spencer:
"""
Fredrik Lundh wrote:
Chris Spencer wrote:
If an XML parser reads in and then writes out a document without having
altered it, then the new document should be the same as the original.

says who?


Good question. There is no One True Answer even within the XML
standards.

It all boils down to how you define "the same". Which parts of the XML
document are meaningful content that needs to be preserved and which
ones are mere encoding variations that may be omitted from the internal
representation?
"""

One can point out the XML namespaces spec all one wants, but it doesn't
matter. The fact is that regardless of what that spec says, as you
say, Chris, there are too many XML technologies that require prefix
retention. As a simple example, XPath and XSLT, W3C specs just like
XMLNS, uses qnames in context, which requires prefix retention.
Besides all that, prefix retention is generally more user friendly in
round-trip or poartial round-trip scenarios.

That's why cDomlette, part of 4Suite [1] and Amara [2], a more Pythonic
API for this, both support prefix retention by default.

[1] http://4suite.org
[2] http://uche.ogbuji.net/tech/4Suite/amara/

--
Uche
http://copia.ogbuji.net

Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: alainpoint | last post by:
Hello, I use Elementtree to parse an elementary SVG file (in fact, it is one of the examples in the "SVG essentials" book). More precisely, it is the fig0201.svg file in the second chapter. The...
3
by: Matthew Thorley | last post by:
Why does ElementTree.parse convert my xsi to an xmlns? When I do this from elementtree import ElementTree # Sample xml mgac =""" <mgac xmlns="http://www.chpc.utah.edu/~baites/mgacML"...
0
by: S ML | last post by:
Hello, I use the Xerces XS* classes to parse and get the definition of an XML schema. I don't seem to find a way to get the namespace prefixes using this framework and I read somewhere that the...
2
by: Wayne Wengert | last post by:
I am exporting an XML file based on a dataset using VB.NET. This works fine but the resulting xml file does not include namespace prefixes, which are required by another tool I am trying to use...
2
by: steve | last post by:
Can someone point me to some information on namespace best practices? Questions I have are as follows: 1) Should I use .NET namespaces or URL, URI and URN's? Ie mycompany.mydivision.myapp vs...
1
by: Hubidubi | last post by:
Hi, I would like to generate XML (struts code) from XML with XSL transformation. I run into problem when I wanted to use tags with prefixes, like <html:text />. I included namespace...
4
by: Samuel R. Neff | last post by:
I'm writing an xslt in vs.net 2003 and in order to get intellisense on the html content I added the default namespace declaration xmlns="http://schemas.microsoft.com/intellisense/ie5". However,...
30
by: Chas Emerick | last post by:
I looked around for an ElementTree-specific mailing list, but found none -- my apologies if this is too broad a forum for this question. I've been using the lxml variant of the ElementTree API,...
6
by: Juha Nieminen | last post by:
Whenever one sees example C++ code basically anywhere, be it in a book, in a tutorial in the internet, in an online forum or whatever, I would estimate that at least in 99% of cases one sees the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.