ElementFilter

Find or remove nodes from ElementTree XML using an XPath-like filter.

New (14th Oct 06): Find or remove attributes - see the doctests for syntax.

Full XPath Implementation: lxml

source, coloured source


 >>> from elementtree import ElementTree
 >>> from elementfilter import findall, data
 >>>
 >>> xml = ElementTree.fromstring( xml_test1 )             
 >>> filter = 'Book[@price<20 and @author=="Hopkins"]/Title'
 >>> print findall(xml, filter)
 [<Element Title at 1199648>, <Element Title at 11996e8>]
 >>>
 >>> xml = ElementTree.fromstring( xml_test2 )             
 >>> data(xml, "channel/item/title")
 ['Normalizing XML, Part 2', 'The .NET Schema Object Model', "SVG's Promising Future"]
 >>> data(xml, "channel/item/creator")
 []
 >>> data(xml, "channel/item/{http://purl.org/dc/elements/1.1/}creator")
 ['Will Provost', 'Priya Lakshminarayanan', 'Antoine Quint']
 >>>
 >>> xml = ElementTree.fromstring( xml_test3 )
 >>> findall( xml, 'category/item[@id=="A001"]/@colour' )
 ['red']
 >>> findall( xml, 'category/item[@id.startswith("A")]/@colour' )
 ['red', 'blue', 'yellow']