ElementTree and namespaces in the header only

Torsten Bronger bronger at physik.rwth-aachen.de
Tue Jan 15 22:01:59 CET 2008


Fredrik Lundh writes:

> Peter Bengtsson wrote:
>> root = Element('feed', xmlns='http://www.w3.org/2005/Atom')
>> root.set('xmlns:se', NS_URL)
>> entry = SubElement(root, 'entry')
>> SubElement(root, 'title').text = 'Title'
>> SubElement(entry, SEN('category')).text = 'Category'
>> But surely the xmlns:se attribute on the <se:category> tag is
>> excessive since the namespace (by the URI) is already defined in the
>> <feed> tag. How do I set non-default namespace tags in elements
>> without the automatic xmlns:se attribute repeated each time?
> ET 1.2's standard serializer doesn't take explicitly set namespaces
> into account.  If you want full control over namespace generation,
> you have to do it yourself:
> http://effbot.org/zone/element-namespaces.htm#explicitly-setting-namespace-attributes
> ET 1.3 provides a default_namespace option for this specific case
> (but you still have to use universal names for everything that
> should go into the default namespace).

I've worked with Saxon for a couple of years, and it tries to
generate the "minimal" (well, sort of) XML file possible: It uses
the prefixes given in the source XSLT file rather than generating
something, and detects implicitly set namespaces, thus avoiding
spitting them out again.  Wouldn't this be an option for ET, too?


Torsten Bronger, aquisgrana, europa vetus
                                      Jabber ID: bronger at jabber.org
               (See http://ime.webhop.org for further contact info.)

More information about the Python-list mailing list