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 replaci
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
tags and then using etree.strip_elements
.
from lxml import etree
data = '''
Some text before
and some text after.
Text before and after
Text after a sibling Text before a sibling
'''
f = etree.fromstring(data)
for r in f.xpath('//r'):
r.tail = 'DELETED' + r.tail if r.tail else 'DELETED'
etree.strip_elements(f,'r',with_tail=False)
print etree.tostring(f,pretty_print=True)
Gives you:
Some text before DELETED
DELETED and some text after.
DELETED
Text before DELETED and after
Text after a sibling DELETED Text before a sibling