I am trying to learn how to make XPath requests from Python using this example XML file: http://pastie.org/1333021 I just added a namespace for this because my application uses it.
Basically, I want to execute a top-level query that returns a subset of nodes and then requests a subset (on a much larger scale than this example)
So, this is my code to first find all the nodes <food>and then iterate over the description of each of them.
import libxml2
doc = libxml2.parseFile("simple.xml")
context = doc.xpathNewContext()
context.xpathRegisterNs("db", "http://examplenamespace.com")
res = context.xpathEval("//db:food")
for node in res:
print "Got Food Node:"
desc = node.xpathEval('db:description')
print desc
So this is a namespace problem, if I remove an attribute xlnsfrom an XML file and use only basic XPATH queries without db:, it works fine. The top query //db:foodworks fine, but the second cannot be evaluated.
, - /.