我已阅读several other questions有关从lxml.etree
删除命名空间的信息,但它们似乎都不适用于默认命名空间(xmlns:blahblahblah
)。
如何删除默认命名空间?它对我的XML处理需求毫无用处,我不想不断地将命名空间映射传递到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>
>>>
方法suggested in this question不起作用,因为在具有默认命名空间的元素上没有前缀。
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