xpath中*、text()和node()区别

3.1k 记录 , , 发表评论

假设有这么一段html:

<div class="post-content">
    <h1>Title</h1>
    <p>Subtitle</p>
    <img src="a.jpg">
    <div>
        <a href="example.html">Goto</a>
    </div>
    Bare text
    <br>
    <!-- this is html comment -->
    <p>Bottom</p>            
</div>

1 child::* 

节点的所有子元素,如//div[@class="post-content"]/*,结果:

<h1>Title</h1>
<p>Subtitle</p>
<img src="a.jpg">
<div>
    <a href="example.html">Goto</a>
</div>
<br>
<p>Bottom</p>

可以看到,这里只选择了有标签名的节点,不在标签内的Bare text和注释都被过滤了。

2 child::text() 

节点的所有文本,如//div[@class="post-content"]/text(),结果:

Bare text

3 child::node() 

节点下的所有内容,不论是标签还是文本还是其他,//div[@class="post-content"]/node(),结果:

    <h1>Title</h1>
    <p>Subtitle</p>
    <img src="a.jpg">
    <div>
        <a href="example.html">Goto</a>
    </div>
    Bare text
    <br>
    <!-- this is html comment -->
    <p>Bottom</p> 

原样输出了其下的所有内容。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

昵称 *