Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Default namespace and attributes with ElementTree 1.3

Reply
Thread Tools

Default namespace and attributes with ElementTree 1.3

 
 
Racinet Georges
Guest
Posts: n/a
 
      06-18-2009
Hi there,

I've got a problem with the way ElementTree 1.3a3-20070912 handles
attributes
with the default namespace feature (please feel free to redirect me if
this isn't the
proper place to discuss such matters).

>>> from elementtree import ElementTree as ET
>>> e = ET.fromstring('<a xmlns="foo"><b id="1"/></a>')
>>> ET.tostring(e)

'<ns0:a xmlns:ns0="foo"><ns0:b id="1" /></ns0:a>'

Notice that the 'id' attribute does not belong to the "foo" namespace.
If I got the XML 1.0 specification right, this is perfectly normal
(Quoting http://www.w3.org/TR/REC-xml-names/#defaulting) :

"Default namespace declarations do not apply directly to attribute
names;
the interpretation of unprefixed attributes is determined by the
element on
which they appear.
(...)
The namespace name for an unprefixed attribute name always has no
value."

Now, trying to serialize this back with the default_namespace feature
of ET 1.3,
I get an error, as predicted by the online documentation

>>> t = ET.ElementTree(e)
>>> t.write(sys.stdout, default_namespace='foo')

Traceback (...)
ValueError: cannot use non-qualified names with default_namespace option

Since this is a parse/serialize round-trip, I think that this
behaviour isn't right, and that
unprefixed attributes should go through. Actually, shouldn't
attributes even be outside
of the scope of the default namespace feature ?

Is there hope for future improvements about this ?
I saw some FIXME comment in the source. The svn head version (rev 52
seems to be identical in that respect, by the way.

Removing in ElementTree.py the part where the ValueError is been raised
seems to solve the issue at first sight :
>>> t.write(sys.stdout, default_namespace='foo')

<a xmlns="foo"><b id="1" /></a>

but this lets also unprefixed elements go through, which is unwanted:
>>> e = ET.fromstring('<a><b id="1"/></a>')
>>> t = ET.ElementTree(e)
>>> t.write(sys.stdout, default_namespace='foo')

<a xmlns="foo"><b id="1" /></a>

I wouldn't care in my application, whose output is xhtml, though.

Regards,
--
Georges Racinet, http://www.racinet.fr
Zope/CPS/Plone expertise, assistance & development
GPG: 0x4862FFF7








-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (Darwin)

iQEVAwUBSjojWeAffZJiF+Z4AQLzpQf+Jf4meVHd5HsB5/DJRMWdSvIT9eCixYXM
WzzW0uXFtF3cJbKptz/KF+elDAdTWvVS3BMPxR/54/Ggz4ItsUR3iBeeh2tH+sp3
OCskxQCpmXvWg9ZfOON26q3M74tXQMnoc5RoSghdLfE0aElwkQ pxJhPaIrQL9+zo
7flFK3cTHcoaOje71m/tN+gA2nK3Tgf3Y2LFYgPbc+hQg9ZhOCQ4TVpIP478hQZY
i5i7S15UZ+Ii0u6XQTHpBchvWdMFiW6jhz353tfeJm6v3tZdN3 G6m/GKGMNfb7+K
rbbhbPDqwp0mWGIlL6rLR12BUtV1F4tOOOV2EneZY/pMn8bcbyV6Aw==
=0tcE
-----END PGP SIGNATURE-----

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ElementTree.XML(string XML) and ElementTree.fromstring(string XML)not working Kee Nethery Python 12 06-27-2009 06:06 AM
ANN: ElementFilter - find and remove elements and attributes from ElementTree XML Gerard Flanagan Python 0 10-14-2006 08:22 PM
Fixed/default attributes in Xerces: Namespace prefix missing?... VanOrton XML 2 11-30-2005 03:17 PM
Reaching into the default namespace when using another namespace. Jason Heyes C++ 1 11-19-2004 02:36 AM
Default namespace and attributes John L. Clark XML 1 12-02-2003 01:12 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57