How can one replace an element with text in lxml?

前端 未结 3 2098
礼貌的吻别
礼貌的吻别 2020-12-16 16:32

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

3条回答
  •  失恋的感觉
    2020-12-16 16:43

    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
    
    

提交回复
热议问题