如何从 python 中的 xmlNode 获取 xpathContext
.net 上的 xpath 和 python 中的 sax 的忠实粉丝,但第一次在 python 中使用 xpath。
我有一个小脚本,它使用 xpath 从文档中选择一些节点,迭代它们,然后理想情况下再次使用 xpath 从中获取相关数据。 然而,我无法得到最后一点,一旦我有了 xmlNode,我就无法从中获取上下文。
import libxml2
import urllib
doc = libxml2.parseDoc(
urllib.urlopen('http://somemagicwebservice.com/').read())
ctxt = doc.xpathNewContext()
listitems = ctxt.xpathEval('//List/ListItem')
for item in listitems:
itemctxt = item.xpathNewContext()
title = itemctxt.xpathEval('//ItemAttributes/Title')
asin = itemctxt.xpathEval('//Item/ASIN')
itemctxc.xpathFreeContext()
ctxt.xpathFreeContext()
doc.freeDoc()
但是,itemctxt = item.xpathNewContext()
位失败,并显示
itemctxt = item.xpathNewContext()
AttributeError: xmlNode instance has no attribute 'xpathNewContext'
Any ideas how to use xpath on a xmlNode? 我在网上找不到任何好的信息。 谢谢
big fan of xpath on .net, and sax in python, but first time using xpath in python.
I have a small script, that uses xpath to select some nodes from a doc, iterates through them, and then ideally uses xpath again to get the relevant data from them. However I can't get that last bit, once I have the xmlNode I cannot get a context from it.
import libxml2
import urllib
doc = libxml2.parseDoc(
urllib.urlopen('http://somemagicwebservice.com/').read())
ctxt = doc.xpathNewContext()
listitems = ctxt.xpathEval('//List/ListItem')
for item in listitems:
itemctxt = item.xpathNewContext()
title = itemctxt.xpathEval('//ItemAttributes/Title')
asin = itemctxt.xpathEval('//Item/ASIN')
itemctxc.xpathFreeContext()
ctxt.xpathFreeContext()
doc.freeDoc()
However the itemctxt = item.xpathNewContext()
bit fails with
itemctxt = item.xpathNewContext()
AttributeError: xmlNode instance has no attribute 'xpathNewContext'
Any ideas how to use xpath on a xmlNode? I can't find any good online info.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为 XPathContext 对元素没有意义? 尝试创建一个新的 XPathContext,并将其节点设置为当前元素。
也就是说,我没有直接使用 libxml2,所以这有点疯狂的猜测。 我通常使用 lxml,它公开了 libxml2 和 libxslt 周围的 ElementTree API。 它更容易使用,并且确实允许在元素上使用 xpath() 。 当然,如果您已经有很多使用 libxml2 的代码,您可能不想切换,但在这种情况下,您可能需要查看 lxmls 源代码以了解它是如何实现的。
http://codespeak.net/svn/lxml/trunk/src/ lxml/xpath.pxi
http://codespeak.net /svn/lxml/trunk/src/lxml/_elementpath.py
似乎是不错的起点。
I don't think an XPathContext makes sense on an element? Try creating a new XPathContext, and setting it's node to the current element.
That said, I haven't used libxml2 directly, so it's a bit of a wild guess. I typically uses lxml, that exposes an ElementTree API around libxml2 and libxslt. It's much easier to use, and does indeed allow xpath() on elements. Of course, if you already have a lot of code using libxml2 you probably don't want to switch, but in that case you might want to take a look at lxmls source to see how it does it.
http://codespeak.net/svn/lxml/trunk/src/lxml/xpath.pxi
http://codespeak.net/svn/lxml/trunk/src/lxml/_elementpath.py
Seems good starting places.
https://stackoverflow.com/a/3379708/288875 建议调用
setContextNode(..) 在新创建的上下文中:
在我当前使用的 python libxml (2.9.1) 版本中,甚至可以调用:
请注意,您必须在 xpath 的开头添加一个点表达式
.//
(而不是//
),否则您将获得相对于文档根目录的搜索结果。https://stackoverflow.com/a/3379708/288875 suggests to call
setContextNode(..)
on a newly created context:In the version of python libxml (2.9.1) which I'm currently using it turns out that one can even call:
Note that you'll have to add a dot at the beginning of the xpath expressions
.//
(instead of//
), otherwise you'll get search results relative to the document root.