No way to delete processing instructions and comments placed outside the tree
Bug #479613 reported by
manu3d
This bug affects 2 people
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
lxml |
Triaged
|
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.:
<?noWayToDelete
<aRoot>
<?thisCanBe
</aRoot>
<?noWayToDelete
There are ways to add them and get them (lxml.etree.
Changed in lxml: | |
importance: | Undecided → Wishlist |
status: | New → Triaged |
To post a comment you must log in.
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 DeletedLikeAllC hildren? > This?>< ?noWayToDeleteT hisEither? ></aRoot>
<aRoot>
<?thisCanBe
<?noWayToDelete
Now you call
root.remove(pi1)
root.remove(pi2)
and you're done!