为什么XPath选择上下文节点之外的节点? [英] Why does XPath select nodes outside of context node?

查看:53
本文介绍了为什么XPath选择上下文节点之外的节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将XPath与Node.js一起使用,并且拥有以下HTML文档,在该文档中,我想选择所有文章节点,然后在第二步中选择所有具有"abc" 类的div:

I'm using XPath with Node.js and I have the following HTML document, where I want to select all article nodes and then in a second step all divs with class "abc":

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test</title>
</head>
<body>
    <article>
        <div>123456</div>
        <div class="abc">Hello0!</div>
    </article>
    <article>
        <div>123456</div>
        <div class="abc">Hello1!</div>
    </article>
    <article>
        <div>123456</div>
        <div class="abc">Hello2!</div>
    </article>
    <article>
        <div>123456</div>
        <div class="abc">Hello3!</div>
    </article>
    <article>
        <div>123456</div>
        <div class="abc">Hello4!</div>
    </article>
    <article>
        <div>123456</div>
        <div class="abc">Hello5!</div>
    </article>
    <article>
        <div>123456</div>
        <div class="abc">Hello6!</div>
    </article>
    <article>
        <div>123456</div>
        <div class="abc">Hello7!</div>
    </article>
    <article>
        <div>123456</div>
        <div class="abc">Hello8!</div>
    </article>
    <article>
        <div>123456</div>
        <div class="abc">Hello9!</div>
    </article>
</body>
</html>

我使用以下代码选择节点:

I used following code to select the nodes:

var xpath = require('xpath');
var DOMParser = require('xmldom').DOMParser;

let parser: DOMParser = new DOMParser();
let doc = parser.parseFromString("HTML-document","text/xml");
let nodes: Node[] = xpath.select("//article", doc);
console.log("NODES: ", nodes.length);
let divs: Node[] = xpath.select("//div[@class='abc']", nodes[0]);
console.log("DIVS: ", divs.length);

我的问题是,在检查两个控制台日志时,第一个显示"NODES:10" .

My problem is, when checking the two console-logs, the first one says "NODES: 10".

到目前为止,我有十个文章节点.但是,当我在十个文章节点中的第一个节点上再次选择时,控制台会显示"DIVS:10" .因此,XPath从一篇文章中选择了所有10个div,我希望其中只有一个 div .

So far, I have ten article nodes. However, when I select again on the first of the ten article nodes, the console says "DIVS: 10". So XPath selected all 10 divs out of one article, where I expected just one div.

我在做什么错了?

推荐答案

您应注意,//表示在页面上的任何位置从开始,而 .//表示从当前节点开始在页面上的任何位置搜索.因此,如果您要从已经找到的 article 元素开始搜索,则需要替换

You should note that // means search wherever on the page starting from root element while .// means search wherever on the page starting from the current node. So if you want to start search from already found article element you need to replace

"//div[@class='abc']"

".//div[@class='abc']"

"./div[@class='abc']"

as div article

这篇关于为什么XPath选择上下文节点之外的节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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