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.

1 Answer 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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.