lxml.objectify drops leading whitespace

Hello, lxml.objectify drops leading whitespace before a tag:
lxml.etree.tostring(lxml.objectify.fromstring('<p> <a>foo</a></p>')) '<p><a>foo</a></p>'
while lxml.etree handles the same situation just fine:
lxml.etree.tostring(lxml.etree.fromstring('<p> <a>foo</a></p>')) '<p> <a>foo</a></p>'
What's the reason for the different behaviour? Is this intentional? Most importantly, how can I make lxml.objectify preserve whitespace correctly? Thanks for your help, Wolfgang

Hi,
Von: Wolfgang Schnerring <ws@gocept.com> Hello,
lxml.objectify drops leading whitespace before a tag:
lxml.etree.tostring(lxml.objectify.fromstring('<p> <a>foo</a></p>')) '<p><a>foo</a></p>'
while lxml.etree handles the same situation just fine:
lxml.etree.tostring(lxml.etree.fromstring('<p> <a>foo</a></p>')) '<p> <a>foo</a></p>'
What's the reason for the different behaviour? Is this intentional? Most importantly, how can I make lxml.objectify preserve whitespace correctly?
It is intentional. For data binding the text content of elements with children is (usually) irrelevant. IIRC this only drops whitespace for elements with children, not for elements with text-only content. You should be able to change objectify's behaviour by changing it's default parser using makeparser() and set_default_parser: def set_default_parser(new_parser = None): u"""set_default_parser(new_parser = None) Replace the default parser used by objectify's Element() and fromstring() functions. The new parser must be an etree.XMLParser. Call without arguments to reset to the original parser. """ def makeparser(**kw): u"""makeparser(remove_blank_text=True, **kw) Create a new XML parser for objectify trees. You can pass all keyword arguments that are supported by ``etree.XMLParser()``. Note that this parser defaults to removing blank text. You can disable this by passing the ``remove_blank_text`` boolean keyword option yourself. """ Holger Landesbank Baden-Wuerttemberg Anstalt des oeffentlichen Rechts Hauptsitze: Stuttgart, Karlsruhe, Mannheim, Mainz HRA 12704 Amtsgericht Stuttgart

Hello and sorry for the long silence, I somehow completely missed your reply.
lxml.objectify drops leading whitespace before a tag:
It is intentional. For data binding the text content of elements with children is (usually) irrelevant.
def makeparser(**kw): u"""makeparser(remove_blank_text=True, **kw)
Perfect, that works just fine. I totally missed that reading the documentation. Thank you very much! Wolfgang
participants (2)
-
Holger Joukl
-
Wolfgang Schnerring