我认为unutbu的XSLT解决方案可能是实现目标的正确方法。
但是,通过修改
<r/>标签的尾部然后使用,这是一种有点棘手的方法
etree.strip_elements。
from lxml import etreedata = '''<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>'''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)
给你:
<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>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)