You can use beautiful soup to do the job :
#!/usr/bin/python # -*- coding: utf-8 -*- import bs4 content = ''' <people> <person born="1975"> <name> <first_name>John</first_name> <last_name>Doe</last_name> </name> <profession>computer scientist</profession> <homepage href="http://www.example.com/johndoe"/> </person> <person born="1977"> <name> <first_name>Jane</first_name> <last_name>Doe</last_name> </name> <profession>computer scientist</profession> <homepage href="http://www.example.com/janedoe"/> </person> </people> ''' from bs4 import BeautifulSoup soup = BeautifulSoup(content) for s in soup('name'): s.extract() print(soup)
It produces the following result :
<html><body><people> <person born="1975"> <profession>computer scientist</profession> <homepage href="http://www.example.com/johndoe"></homepage> </person> <person born="1977"> <profession>computer scientist</profession> <homepage href="http://www.example.com/janedoe"></homepage> </person> </people> </body></html>
With namespaces :
#!/usr/bin/python # -*- coding: utf-8 -*- import bs4 content = '''<people xmlns:h="http://www.example.com/to/"> <h:person born="1975"> <h:name> <h:first_name>John</h:first_name> <h:last_name>Doe</h:last_name> </h:name> <h:profession>computer scientist</h:profession> <h:homepage href="http://www.example.com/johndoe"/> </h:person> <h:person born="1977"> <h:name> <h:first_name>Jane</h:first_name> <h:last_name>Doe</h:last_name> </h:name> <h:profession>computer scientist</h:profession> <h:homepage href="http://www.example.com/janedoe"/> </h:person> </people> ''' from bs4 import BeautifulSoup soup = BeautifulSoup(content).people for s in soup('h:name'): s.extract() print(soup)
I added .people
to prevent <html><body>
</body></html>
in the result.
<people xmlns:h="http://www.example.com/to/"> <h:person born="1975"> <h:profession>computer scientist</h:profession> <h:homepage href="http://www.example.com/johndoe"></h:homepage> </h:person> <h:person born="1977"> <h:profession>computer scientist</h:profession> <h:homepage href="http://www.example.com/janedoe"></h:homepage> </h:person> </people>
tag
?