编程语言
首页 > 编程语言> > Python libxml2 XPath /命名空间帮助

Python libxml2 XPath /命名空间帮助

作者:互联网

我试图学习如何使用以下示例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属性并仅使用基本的XPATH查询而不使用db,那么这实际上是一个名称空间问题:它可以正常工作.最上面的查询// db:food可以正常工作,但是第二个查询评估失败.

请有人可以更正我的名称空间/查询语法.

非常感谢

解决方法:

我通常不使用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>

标签:python,libxml2
来源: https://codeday.me/bug/20191023/1914830.html