Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stripping spaces between XML nodes with Python

Tags:

python

xml

xslt

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?

like image 368
Atlas1j Avatar asked Nov 22 '25 09:11

Atlas1j


1 Answers

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)
like image 83
Alex Martelli Avatar answered Nov 23 '25 22:11

Alex Martelli