[lxml-dev] An lxml tree inside a lxml tree.
Stefan Behnel
stefan_ml at behnel.de
Sun Mar 23 17:57:27 CET 2008
Hi,
John Lovell wrote:
> Okay, I came up with some sample code to illustrate my problem and the
> behavior shows up without included one tree inside another.
>
>
> Code:
>
> from lxml import etree
> from StringIO import StringIO
>
> xml = """<root>
> <child1/>
> <child2/>
> <child3/>
> </root>
> """
>
> # It doesn't matter which one of these I use.
> root = etree.fromstring(xml)
> #root = etree.parse(StringIO(xml)).getroot()
>
> print root.tag
>
> print ""
>
> print etree.tostring(root, pretty_print=False)
>
>
> Output:
>
> root
>
> <root>
> <child1/>
> <child2/>
> <child3/>
> </root>
lxml will not alter your document unless you tell it to do so. This includes
whitespace - which you may or may not consider ignorable. The
"pretty_print=False" option tells lxml to not modify the document on
serialisation, so it doesn't keeps the whitespace the way it is.
If you want the whitespace removed, tell the parser to do it for you using the
"remove_blank_text" option.
Stefan
More information about the lxml-dev
mailing list