[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

ElementTree and namespaces in the header only

peterbe@gmail.com

1/15/2008 2:30:00 PM

Here's my code (simplified):

NS_URL = 'http://www.snapex...ato...
ElementTree._namespace_map[NS_URL] = 'se'
def SEN(tag):
return "{%s}%s" % (NS_URL, tag)

root = Element('feed', xmlns='http://www.w3.org/2005...)
root.set('xmlns:se', NS_URL)
entry = SubElement(root, 'entry')
SubElement(root, 'title').text = 'Title'
SubElement(entry, SEN('category')).text = 'Category'


And here's the generated XML string:

<feed xmlns="http://www.w3.org/2005/... xmlns:se="http://
www.snapexpense.com/atom_ns#">
<entry>
<se:category xmlns:se="http://www.snapex...
atom_ns#">Category</se:category>
</entry>
<title>Title</title>
</feed>


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?
3 Answers

Fredrik Lundh

1/15/2008 5:22:00 PM

0

Peter Bengtsson wrote:

> root = Element('feed', xmlns='http://www.w3.org/2005...)
> 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-...

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

</F>

Torsten Bronger

1/15/2008 9:02:00 PM

0

Hallöchen!

Fredrik Lundh writes:

> Peter Bengtsson wrote:
>
>> root = Element('feed', xmlns='http://www.w3.org/2005...)
>> 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-...
>
> 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?

Tschö,
Torsten.

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

peterbe@gmail.com

1/17/2008 12:14:00 AM

0

On Jan 15, 5:22 pm, Fredrik Lundh <fred...@pythonware.com> wrote:
> Peter Bengtsson wrote:
> > root = Element('feed', xmlns='http://www.w3.org/2005...)
> > 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 setnamespacesinto
> account. If you want full control over namespace generation, you have
> to do it yourself:
>
> http://effbot.org/zone/element-namespaces.htm#explicitly-se......
>
> 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).
>
> </F>

That's perfect Fredrik. Exactly what I was looking for.