Monday, February 4, 2013

lxml cannot use absolute path on element

It turns out when you use lxml.fromstring(XML), it returns back the element instead of a document tree (unlike lxml.parse()).  You have to do lxml.fromstring(XML).getroottree().  This way, doing findall() will avoid triggering the "cannot use absolute path on element" error.

http://www.inductiveload.com/solutions-to-errors/

LXML

Error: SyntaxError: cannot use absolute path on element
Problem: I was trying to do:
root = doc.getroot()
tags = root.findall("//tagname")

Solution: You need to use the XML document directly to use an absolute path:
tags = doc.findall("//g")

4 comments: