
Holger Joukl wrote:
Stefan Behnel wrote:
That leads to the problem I pointed out at the top. What is your actual reasoning for requiring that empty leaf elements should be StringElements? I mean, you could always make them StringElements explicitly by setting
root.a.b.c.d = ''
Wouldn't this end up in d being an ObjectifiedElement if the logic (empty leaves are StringElements) changed?
No. The value is an empty string, not an empty value. So there is text content in there, it's just of length zero.
When parsing from XML I need '<root><s>some string</s></root>' to behave like '<root><s></s></root>'. For someone processing the data "s" should always act like a (possibly empty) string. Your solution would only work for me if ObjectifiedElement got a .pyval attribute, too, and its .text was not None but rather '' if no text content is in the node, and probably also needed the String API parts.
Much of this stems from the fact the ElementTree elt.text returns None if there is no element text instead of '' (but I guess this won't change :-)
It returns '' if the value is '' and it returns None if there is no value. That already changed to adapt to ET's own behaviour. The parser sees "<a/>" and "<a></a>" as not having a value. So you will never get an empty string back from a parsed tree. However, if you set it to '', lxml will continue to return an empty string and objectify will determine that it is a StringElement. Maybe you could get by with wrapper functions that add the '' for leafs where required? Stefan