No way to delete processing instructions and comments placed outside the tree

Bug #479613 reported by manu3d on 2009-11-09
10
This bug affects 2 people
Affects Status Importance Assigned to Milestone
lxml
Wishlist
Unassigned

Bug Description

It appears that there isn't a way to delete a processing instruction or a comment listed before or after the tree. I.e.:

<?noWayToDeleteThis?>
<aRoot>
    <?thisCanBeDeletedLikeAllChildren?>
</aRoot>
<?noWayToDeleteThisEither?>

There are ways to add them and get them (lxml.etree._Element methods addnext(), getnext(), addprevious() getprevious(), but it appears that a couple of methods should be available and are missing: removenext() and removeprevious().

scoder (scoder) on 2011-08-12
Changed in lxml:
importance: Undecided → Wishlist
status: New → Triaged
Olli Pottonen (olli-pottonen) wrote :

Yes there is a way to remove them, although it's not as straightforward as it should. Unlike some other XML libraries, lxml allows an element to occur in one place. That is, you can not copy an element to another place; attempting that moves it to the new location, removing it from the old.

So, let root be the root element (lxml.etree._Element object) in the example given by manu3d.
Call

pi1 = root.getprevious()
pi2 = root.getnext()
root.append(pi1)
root.append(pi2)

Now the tree is
<aRoot>
    <?thisCanBeDeletedLikeAllChildren?>
<?noWayToDeleteThis?><?noWayToDeleteThisEither?></aRoot>

Now you call

root.remove(pi1)
root.remove(pi2)

and you're done!

To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers