[lxml-dev] lxml replace() deletes tail
Stefan Behnel
behnel_ml at gkec.informatik.tu-darmstadt.de
Mon Oct 2 11:35:43 CEST 2006
Hi,
Chris Abraham wrote:
> We have a question about the etree.replace() function. We found that it
> doesn't preserve the tail text from the replaced node when inserting a
> new node. Perhaps this is the intended behavior, but, to us, it was
> unexpected. In the example below, notice how the "tail" text is deleted
> when the <p> is replaced:
>
>>>> tree = etree.HTML("<html><body>text
> before<p>textin</p>tail</body></html>")
>>>> newel = etree.HTML("<html><body><div>new</div></body></html>")
>>>> tree[0].replace(tree[0][0], newel[0][0])
>>>> etree.tostring(tree)
> '<html><body>text before<div>new</div></body></html>'
That *is* the expected behaviour. :)
When you replace the element "<p>textin</p>tail" with the element
"<div>new</div>" you get "<div>new</div>".
Note that the tail is a property of the element, so it would rather be
unexpected if the replaced element copied its own tail over to the new element.
You can always copy the tail from the original element by hand, in case you
need to.
Stefan
More information about the lxml-dev
mailing list