views:

48

answers:

3

Having following Python code:

>>> from lxml import etree
>>> root = etree.XML("<a><b></b></a>")
>>> etree.tostring(root)
'<a><b/></a>'

How can I force lxml to use "long" version?

Like

>>> etree.tostring(root)
'<a><b></b></a>'
A: 

no sure why you would want this

Philippe Ombredanne
As said - testing and diffs.
Almad
+2  A: 

Why do you want to? Both are equivalent in terms of XML's data model.

djc
Yes, but in case of testing (which is what I do), string representation of the model might matter.
Almad
+3  A: 
>>> import lxml.html
>>> html = lxml.html.fromstring('<a><b></b></a>')
>>> lxml.html.tostring(html)
'<a><b></b></a>'

Mixing works as well:

>>> from lxml import etree
>>> import lxml.html
>>> xml = etree.XML('<a><b/></a>')
>>> lxml.html.tostring(xml)
'<a><b></b></a>'
newtover