I've read several other questions about removing namespaces from an lxml.etree
but none of them seems to work on default namespace (xmlns:blahblahblah
).
How can I remove the default namespace? It has no use for my XML processing needs and I don't want to have to continually pass in a namespace map into somenode.xpath('.//default:sometag', namespaces=my_annoying_namespace_map)
>>> from lxml import etree, objectify
>>> xml = etree.fromstring('''<forest xmlns="idontcare.com">
<tree><branch><leaf>bill</leaf></branch><branch><leaf>bob</leaf></branch></tree>
<tree><branch><leaf>sue</leaf></branch></tree></forest>''')
>>> xml
<Element {idontcare.com}forest at 0x2aa0288>
>>> objectify.deannotate(xml, cleanup_namespaces=True)
>>> xml
<Element {idontcare.com}forest at 0x2aa0288>
>>>
The method suggested in this question doesn't work because there is no prefix on elements with default namespaces.
def strip_ns_prefix(tree):
#iterate through only element nodes (skip comment node, text node, etc) :
for element in tree.xpath('descendant-or-self::*'):
#if element has prefix...
if element.prefix:
#replace element name with it's local name
element.tag = etree.QName(element).localname
return tree