Is there a easy way to accomplish the same thing in Python as xsl accomplishes with:
<xsl:strip-space elements="*"/>
So for instance in the following
for event, elem in ElementTree.iterparse("/tmp/example.xml"):
if elem.tag == "example":
print ElementTree.tostring(elem)
when the example nodes are printed out all the spaces and line feeds in the input file between children of the example node will be removed?
I believe you need to explicitly manipulate the subtree to strip every text and tail:
from xml.etree import ElementTree
for event, elem in ElementTree.iterparse("/tmp/example.xml"):
if elem.tag == "example":
subiter = ElementTree.ElementTree(elem).getiterator()
for x in subiter:
if x.text: x.text = x.text.strip()
if x.tail: x.tail = x.tail.strip()
print ElementTree.tostring(elem)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With