etree.cleanup_namespaces does not remove a wrapping namespace in an unwrapped subtree

Bug #1996268 reported by Sebastian Albert
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
lxml
New
Undecided
Unassigned

Bug Description

This may either be a bug or a misunderstanding on my side.

I have an XML document with a wrapper namespace, and I am extracting the payload from a subtree. I want to get rid of the wrapper namespace, but using etree.cleanup_namespaces does not yield the expected result.

Here is a short example REPL session:

>>> from lxml import etree
>>> tree = etree.fromstring('<wrap:moep xmlns:wrap="http://example.com/wrap"><foo xmlns="http://example.com/content"><bar /></foo></wrap:moep>')
>>> tree
<Element {http://example.com/wrap}moep at 0x7f246363aec0>
>>> etree.tostring(tree)
b'<wrap:moep xmlns:wrap="http://example.com/wrap"><foo xmlns="http://example.com/content"><bar/></foo></wrap:moep>'
>>> foo = tree.find('{http://example.com/content}foo')
>>> etree.tostring(foo)
b'<foo xmlns="http://example.com/content" xmlns:wrap="http://example.com/wrap"><bar/></foo>'
>>> etree.cleanup_namespaces(foo)
>>> etree.tostring(foo)
b'<foo xmlns="http://example.com/content" xmlns:wrap="http://example.com/wrap"><bar/></foo>'

I would expect the xmlns:wrap to have been removed. Am I missing something, or is this a bug?

Python : sys.version_info(major=3, minor=8, micro=13, releaselevel='final', serial=0)
lxml.etree : (4, 9, 1, 0)
libxml used : (2, 9, 14)
libxml compiled : (2, 9, 14)
libxslt used : (1, 1, 35)
libxslt compiled : (1, 1, 35)

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.