You can accomplish this using etree.strip_attributes
and etree.cleanup_namespaces
.
In [8]: etree.strip_attributes(foo, '{http://codespeak.net/lxml/objectify/pytype}pytype') In [9]: print etree.tostring(foo, pretty_print=True) <foo xmlns:py="http://codespeak.net/lxml/objectify/pytype" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <bar>hi</bar> <baz>1</baz> <fritz xsi:nil="true"/> </foo> In [10]: etree.cleanup_namespaces(foo) In [11]: print etree.tostring(foo, pretty_print=True) <foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <bar>hi</bar> <baz>1</baz> <fritz xsi:nil="true"/> </foo>
This still leaves the xsi:nil
link, which you can remove in the same way.
In [12]: etree.strip_attributes(foo, '{http://www.w3.org/2001/XMLSchema-instance}nil') In [13]: etree.cleanup_namespaces(foo) In [14]: print etree.tostring(foo, pretty_print=True) <foo> <bar>hi</bar> <baz>1</baz> <fritz/> </foo>
source share