My bad, it's more about the text/tail handling of these two functions. I've clarified the original question http://stackoverflow.com/questions/23282241/lxml-difference-between-element-... Although I'm aware of the comment on addnext() I still don't quite see the comment align with what's happening, ie the tail is not actually discarded from <b> but appended to tail of <i>. Why is that? And what's the intention of discarding tail anyway? Cheers, Jens On Fri, Apr 25, 2014 at 03:10:38PM +0200, Holger Joukl wrote:
Hi,
Von: Jens Tröger
I've been scratching my head over the following question, and can't quite figure this out. Any comments are appreciated :) http://stackoverflow.com/questions/23282241/lxml-difference-between-element-...
?
Works just as documented:
root = etree.fromstring('<root><a/><b/><c/></root>') print etree.tostring(root, pretty_print=True) <root> <a/> <b/> <c/> </root>
child = root[0] help(root[0].addnext) Help on built-in function addnext:
addnext(...) addnext(self, element)
Adds the element as a following sibling directly after this element.
This is normally used to set a processing instruction or comment after the root node of a document. Note that tail text is automatically discarded when adding at the root level.
child.addnext(etree.Element('a_addnext')) print etree.tostring(root, pretty_print=True) <root> <a/>
<b/> <c/> </root> help(root[0].addprevious) Help on built-in function addprevious:
addprevious(...) addprevious(self, element)
Adds the element as a preceding sibling directly before this element.
This is normally used to set a processing instruction or comment before the root node of a document. Note that tail text is automatically discarded when adding at the root level.
child.addprevious(etree.Element('a_addprevious')) print etree.tostring(root, pretty_print=True) <root>
<a/> <b/> <c/> </root> root.insert(root.index(child), etree.Element('root_insert')) print etree.tostring(root, pretty_print=True) <root>
<a/> <b/> <c/> </root> Maybe you just confused that addnext() and addprevious() add siblings and insert() inserts children?
root = etree.fromstring('<root><a/><b/><c/></root>') child = root[0] child.addprevious(etree.Element('before_a')) # equivalent: root.insert (root.index(child), etree.Element('before_a'))
child.addnext(etree.Element('after_a')) # equivalent: root.insert (root.index(child) + 1, etree.Element('after_a'))
Regards Holger
Landesbank Baden-Wuerttemberg Anstalt des oeffentlichen Rechts Hauptsitze: Stuttgart, Karlsruhe, Mannheim, Mainz HRA 12704 Amtsgericht Stuttgart
_________________________________________________________________ Mailing list for the lxml Python XML toolkit - http://lxml.de/ lxml@lxml.de https://mailman-mail5.webfaction.com/listinfo/lxml
-- Jens Tröger http://savage.light-speed.de/