P
Peter Bengtsson
Here's my code (simplified):
NS_URL = 'http://www.snapexpense.com/atom_ns#'
ElementTree._namespace_map[NS_URL] = 'se'
def SEN(tag):
return "{%s}%s" % (NS_URL, tag)
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'
And here's the generated XML string:
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:se="http://
www.snapexpense.com/atom_ns#">
<entry>
<se:category xmlns:se="http://www.snapexpense.com/
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?
NS_URL = 'http://www.snapexpense.com/atom_ns#'
ElementTree._namespace_map[NS_URL] = 'se'
def SEN(tag):
return "{%s}%s" % (NS_URL, tag)
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'
And here's the generated XML string:
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:se="http://
www.snapexpense.com/atom_ns#">
<entry>
<se:category xmlns:se="http://www.snapexpense.com/
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?