使用 XPath 在上下文节点下查找节点不起作用(firefox/firebug/javascript) [英] using XPath to find node under a context node does not work (firefox/firebug/javascript)

查看:31
本文介绍了使用 XPath 在上下文节点下查找节点不起作用(firefox/firebug/javascript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从网页中提取信息.

I want to extract information from a web page.

页面有 m 个节点,可以通过 .evaluate("//div[@class='news']", document, ....) 找到.

The page has m nodes, which can be found by .evaluate("//div[@class='news']", document, ....).

对于上述每个节点,它们内部都有 3 个节点.他们每个人都有不同的@class 选择器.我想提取这些 m 个三元组记录.

For each of the above nodes, there are 3 nodes inside them. Each of them has different @class selector. And I want to extract these m 3-tuple records.

我尝试按照说明使用 .evaluate() 函数

I tried to use .evaluate() function as instructed in

https://developer.mozilla.org/en/Introduction_to_using_XPath_in_JavaScript

使用此代码

parentNodes = document.evaluate("//div[@class='news']", document, ....).
while (true){
   var node = parentNodes.iterateNext();
   var child = document.evaluate("//div[@class='title']", node, ....). 
   ...
}

然而,child"总是分配给文档中的第一个节点,而不是node"中的第一个节点.

However, "child" is always assigned to the first node in the document, instead of the first node within "node".

我在萤火虫控制台中运行了这个.

I ran this in firebug console.

有人知道怎么回事吗?

推荐答案

您正在对文档调用评估.因此,正在从 XML 树的根计算 XPath 表达式.此外,如果您希望 XPath 从当前上下文中选择一个节点,例如在当前节点的子节点中,您应该使用 .// 上下文选择器.

You are calling evaluate on the document. Hence, the XPath expression is being evaluated from the root of the XML tree. Also, if you want XPath to select a node from within the current context, e.g. among the children of the current node, you should use the .// context selector.

这篇关于使用 XPath 在上下文节点下查找节点不起作用(firefox/firebug/javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆