2

I am trying to remove an element in an xml which contains a namespace. Here is my code:

templateXml = """<?xml version="1.0" encoding="UTF-8"?>
<Metadata xmlns="http://www.amazon.com/UnboxMetadata/v1">
<Movie>
        <CountryOfOrigin>US</CountryOfOrigin>
        <TitleInfo>
                <Title locale="en-GB">The Title</Title>
                <Actor>
                        <ActorName locale="en-GB">XXX</ActorName>
                        <Character locale="en-GB">XXX</Character>
                </Actor>
        </TitleInfo>    
</Movie>
</Metadata>"""

from lxml import etree
tree = etree.fromstring(templateXml)

namespaces = {'ns':'http://www.amazon.com/UnboxMetadata/v1'}

for checkActor in tree.xpath('//ns:Actor', namespaces=namespaces):
    etree.strip_elements(tree, 'ns:Actor')

In my actual XML I have lots of tags, So I am trying to search for the Actor tags which contain XXX and completely remove that whole tag and its contents. But it's not working.

4

1 回答 1

2

Use remove() method:

for checkActor in tree.xpath('//ns:Actor', namespaces=namespaces):
    checkActor.getparent().remove(checkActor)

print etree.tostring(tree, pretty_print=True, xml_declaration=True)

prints:

<?xml version='1.0' encoding='ASCII'?>
<Metadata xmlns="http://www.amazon.com/UnboxMetadata/v1">
<Movie>
        <CountryOfOrigin>US</CountryOfOrigin>
        <TitleInfo>
                <Title locale="en-GB">The Title</Title>
                </TitleInfo>    
</Movie>
</Metadata>
于 2013-08-17T19:56:13.840 回答