合并 document.evaluate 中的 XPathResults
我正在执行一堆 document.evaluate,然后使用 result.snapshotLength
上的 for 循环迭代每个结果。
由于我在每个循环(thisDiv.parentNode.removeChild
)内执行相同的操作,因此我只想执行一个循环。
我读过:
第五个参数可用于 合并两个 XPath 的结果 查询。传入 a 的结果 之前对 document.evaluate 的调用, 它将返回合并后的 两个查询的结果
所以我尝试了:
comDivs = document.evaluate(
"//div[@class='class name 1']",
document,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
null);
ggDivs = document.evaluate(
"//div[@class='class name 2']",
document,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
comDivs);
但这不起作用(虽然我没有错误日志,但它不起作用)。
这样做的正确方法是什么?我可以运行不同的 XPath 查询并合并结果吗?或者有没有办法将正则表达式或某种替换传递给查询本身?
我现在的代码位于: http://userscripts.org/scripts/review/58939
感谢您的帮助!
I'm doing a bunch of document.evaluate then iterating through each result with a for loop on result.snapshotLength
.
Since I do the same thing inside each loop (a thisDiv.parentNode.removeChild
) I would like to do just one loop.
I've read that :
The fifth parameter can be used to
merge the results of two XPath
queries. Pass in the result of a
previous call to document.evaluate,
and it will return the combined
results of both queries
So I tried :
comDivs = document.evaluate(
"//div[@class='class name 1']",
document,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
null);
ggDivs = document.evaluate(
"//div[@class='class name 2']",
document,
null,
XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
comDivs);
But this doesn't work (although I don't have an error log, it just doesn't work).
What's the proper way of doing that? Can I run different XPath queries and merge the results? Or is there a way to pass regular expressions or some kind of alternation to the query itself?
The code I have for now is at : http://userscripts.org/scripts/review/58939
Thanks for your help !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以简化用户脚本中的 XPath 并使用“
|
”连接单个表达式:或者在具有严格条件的一个表达式中更好:
对于上面的示例,iirc 第二个查询只会找到匹配项,如果第一个查询的结果包含它们,即 if
//div[@class='class name 2']
其中第一个查询的结果节点的子节点//div[@class ='类名2']
。You can simplify your XPath in the userscript and join single expressions with "
|
":or even better in one expression with severel conditions:
As for your example above, iirc the second query would only find matches, if the result f the first query contained them, i.e. if
//div[@class='class name 2']
where children of the result nodes from the first query//div[@class='class name 2']
.