Let's have this in an XML file (in.xml
):
<?xml version="1.0" encoding="ASCII"?>
<a>
<b>
<c>abc</c>
</b>
</a>
Now, I run this code to strip the tag <b>
and write the result back in a file:
from lxml import etree
tree = etree.parse('in.xml', parser=etree.XMLParser())
root = tree.getroot()
etree.strip_tags(root, 'b')
tree.write('out.xml', xml_declaration=True)
This output file (out.xml
) looks like this:
<?xml version='1.0' encoding='ASCII'?>
<a>
<c>abc</c>
</a>
How can I remove the blank lines left by the stripped tag?