etree.tostring(): XML pretty printing does not work on non-indented XML files.
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
lxml |
Won't Fix
|
Low
|
scoder |
Bug Description
Pretty printing an XML file via lxml.etree.
These means no indentation will be applied corresponding lines/elements according to the XML tree depth level.
EXAMPLE:
from lxm import etree
from lxm import objectify
xmldoc = """\
<root>
<alice />
<bob />
</root>
"""
root = etree.fromstrin
formatted = etree.tostring(
expected = """\
<root>
<alice />
<bob />
</root>
"""
assert expected == formatted, "OOPS, fails."
# -- VARIANT 2: formatted = etree.tostring(
WORKAROUND:
Formatting/pretty printing works when you create the "root" object via lxml.objectify.
Just replace the following line in the example from above:
root = objectify.
VERSION-INFO:
Python : (2, 6, 6, 'final', 0)
lxml.etree : (2, 3, 2, 0)
libxml used : (2, 7, 3)
libxml compiled : (2, 7, 3)
libxslt used : (1, 1, 24)
libxslt compiled : (1, 1, 24)
This doesn't really have much to do with lxml.
http:// lxml.de/ FAQ.html# why-doesn- t-the-pretty- print-option- reformat- my-xml- output
I'm inclined to close this as "won't fix". While lxml could potentially influence the heuristic here, I don't think there is a right way to do it.