[lxml-dev] Whitespace foiling pretty_print - any fix?

B Wooster bwooster47 at gmail.com
Thu Jul 30 19:47:42 CEST 2009


f an XML element has whitespace along with sub elements, pretty_print
does not work well.

This is probably a XML whitespace issue, but is there any way to work
around this issue?

Here's example code and the output it prints, and what I would like to get:

from lxml import etree as ET
root = ET.XML("""<a>
       <b>test</b>
       </a>""")
ET.SubElement(root, "c")
ET.SubElement(root, "d")
print ET.tostring(root, pretty_print=True)
# This prints:
"""<a>
       <b>test</b>
       <c/><d/></a>
"""
# Would like:
"""<a>
       <b>test</b>
       <c/>
       <d/>
  </a>
"""


More information about the lxml-dev mailing list