I want to use Python and lxml to generate XML like the following:
<root xmlns="foo">
<bar />
</root>
However, the following code creates XML that is semantically identical, but uses ugly auto-generated namespace prefixes instead:
from lxml import etree
root = etree.Element('{foo}root')
etree.SubElement(root,'{foo}bar')
print(etree.tostring(root))
#=> b'<ns0:root xmlns:ns0="foo"><ns0:bar/></ns0:root>'
How do I get lxml/etree to generate XML using a single default namespace on the root element, with no namespace prefixes on any descendant elements?