I am parsing an xml file generated by an external program. I would then like to add custom annotations to this file, using my own namespace. My input looks as below:
If you temporarily add a namespaced attribute to the root node, that does the trick.
ns = '{http://this.is.some/custom_namespace}'
# add 'kjw:foobar' attribute to root node
root.set(ns+'foobar', 'foobar')
# add kjw namespace elements (or attributes) elsewhere
... get child element species ...
species.append(etree.Element(ns + 'test'))
# remove temporary namespaced attribute from root node
del root.attrib[ns+'foobar']