It's easy to completely remove a given element from an XML document with lxml's implementation of the ElementTree API, but I can't see an easy way of consistently replacing an element with some text. For example, given the following input:
input = '''<everything>
<m>Some text before <r/></m>
<m><r/> and some text after.</m>
<m><r/></m>
<m>Text before <r/> and after</m>
<m><b/> Text after a sibling <r/> Text before a sibling<b/></m>
</everything>
'''
... you could easily remove every <r>
element with:
from lxml import etree
f = etree.fromstring(data)
for r in f.xpath('//r'):
r.getparent().remove(r)
print etree.tostring(f, pretty_print=True)
However, how would you go about replacing each element with text, to get the output:
<everything>
<m>Some text before DELETED</m>
<m>DELETED and some text after.</m>
<m>DELETED</m>
<m>Text before DELETED and after</m>
<m><b/>Text after a sibling DELETED Text before a sibling<b/></m>
</everything>
It seems to me that because the ElementTree API deals with text via the .text
and .tail
attributes of each element rather than nodes in the tree, this means you have to deal with a lot of different cases depending on whether the element has sibling elements or not, whether the existing element had a .tail
attribute, and so on. Have I missed some easy way of doing this?
Using ET.XSLT:
yields
I think that unutbu's XSLT solution is probably the correct way to achieve your goal.
However, here's a somewhat hacky way to achieve it, by modifying the tails of
<r/>
tags and then usingetree.strip_elements
.Gives you:
Using
strip_elements
has the disadvantage that you cannot make it keep some of the<r>
elements while replacing others. It also requires the existence of anElementTree
instance (which may be not the case). And last, you cannot use it to replace XML comments or processing instructions. The following should do your job: