Unable to find element with xpath
I have an XML document that looks like this: https://drive.google.com/file/d/0B4basSIXW5iGNUVfb01ZQkJQUzQ/view?usp=sharin... I've previously used lxml for parsing html documents and would like to use it for XML documents as well. I am interested in finding all the "Lap" elements in the XML file and have written some simple python code: sourcefile = f1=open('NAME_AND_PATH_OF_XML', 'r') sourceXML = sourcefile.read() root = etree.fromstring(sourceXML) laps = root.xpath('//Lap') print len(laps) For some reason it cannot find "Lap" in the XML. The XML seems to be valid when I open it with a browser. Any suggestions? Best regards Ziggy999
On Fri, May 06, 2016 at 08:26:00PM +0200, Stein Rune Risa wrote:
I have an XML document that looks like this:
https://drive.google.com/file/d/0B4basSIXW5iGNUVfb01ZQkJQUzQ/view?usp=sharin...
I've previously used lxml for parsing html documents and would like to use it for XML documents as well.
I am interested in finding all the "Lap" elements in the XML file and have written some simple python code:
sourcefile = f1=open('NAME_AND_PATH_OF_XML', 'r') sourceXML = sourcefile.read() root = etree.fromstring(sourceXML) laps = root.xpath('//Lap') print len(laps)
For some reason it cannot find "Lap" in the XML. The XML seems to be valid when I open it with a browser.
Because XML namespaces are a thing! Try GARMIN_NS = 'http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2' laps = root.xpath('//{%s}Lap' % GARMIN_NS) See http://lxml.de/tutorial.html#namespaces for more details. HTH, Marius Gedminas -- Moore's Law, I need hardly remind a top-notch industry professional like you, states that as the density of silicon circuitry doubles, the probability of you not being able to find some sensibly-priced extra memory to fit your old lappy approaches 1.0. -- Verity Stob
participants (2)
-
Marius Gedminas
-
Stein Rune Risa