I’m trying to learn how to do XPath queries from Python using this sample XML file: http://pastie.org/1333021 I just added a namespace to it because my actual application uses it.
Basically, I want to execute a top level query that returns a subset of nodes and then query the subset (on a much larger scale than this example)
So this is my code to first find all <food> nodes and then iterate over the description of each one.
#!/usr/bin/python2
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:
# Query xmlNode here
print "Got Food Node:"
desc = node.xpathEval('db:description') # this is wrong?
print desc
So it’s essentially a namespace problem, if I remove the xlns attribute from the XML file and use just basic XPATH queries without db: it works fine. The top query //db:food works fine, but the second one fails evaluating.
Please can someone correct my namespace/query syntax.
Thanks a lot
I don’t normally use libxml2, I much prefer lxml.etree.
Had a play around. The
xpathEvalmethod on your node creates a new context each time, apparently without the namespace you registered.You can reset your context to different locations like this: