我正在尝试使用此示例XML文件学习如何从Python执行XPath查询:http://pastie.org/1333021我刚刚为它添加了一个命名空间,因为我的实际应用程序使用它。
基本上,我想执行一个返回节点子集的顶级查询,然后查询子集(比这个例子大得多)
所以这是我的代码,首先找到所有<food>
个节点,然后迭代每个节点的描述。
#!/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
所以它本质上是命名空间问题,如果我从XML文件中删除xlns
属性并且只使用没有db:
的基本XPATH查询就可以了。顶部查询//db:food
工作正常,但第二个查询无法评估。
请有人纠正我的命名空间/查询语法。
非常感谢
答案 0 :(得分:5)
我通常不使用libxml2,我更喜欢lxml.etree。
玩了一下。节点上的xpathEval
方法每次都会创建一个新的上下文,显然没有您注册的命名空间。
您可以将上下文重置为不同的位置,如下所示:
>>> import libxml2
>>> from urllib2 import urlopen
>>> data = urlopen('http://pastie.org/pastes/1333021/download').read()
>>>
>>> doc = libxml2.parseMemory(data,len(data))
>>>
>>> context = doc.xpathNewContext()
>>> context.xpathRegisterNs("db", "http://examplenamespace.com")
0
>>>
>>> for res in context.xpathEval("//db:food"):
... context.setContextNode(res)
... print "Got Food Node:"
... desc = context.xpathEval('./db:description')[0]
... print desc
...
Got Food Node:
<description>two of our famous Belgian Waffles with plenty of real maple syrup</description>
Got Food Node:
<description>light Belgian waffles covered with strawberries and whipped cream</description>
Got Food Node:
<description>light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
Got Food Node:
<description>thick slices made from our homemade sourdough bread</description>
Got Food Node:
<description>two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>