[Python+lxml] Remove nodes with specific name?


I’m sure there are people here who know Python and lxml.

I want to remove all occurences of the “time” nodes from a GPX file.

This does nothing:

import lxml.etree as et

tree = et.parse("input.gpx")

for elem in tree.xpath( '//time' ) :

with open("output.gpx", 'wb') as doc:
   doc.write(et.tostring(tree, pretty_print = True)) 

What’s the right way?

Thank you.

Edit: For others’ benefit — although I’m still pretty much in the dark when it comes to XML parsers:

#AttributeError: 'xml.etree.ElementTree.Element' object has no attribute 'getparent'
#import xml.etree.ElementTree as ET
import lxml.etree as ET

tree = ET.parse('input.gpx')
#Must prepend namespace; There might be a way to tell ET to search while ignoring it
ns = {"sm": "http://www.topografix.com/GPX/1/1"}

for elem in tree.findall(".//sm:time", ns):
with open("removed.time.gpx", 'wb') as doc:
   doc.write(ET.tostring(tree, pretty_print = True))