xpath中*、text()和node()区别
假设有这么一段html:
<div> <h1>Title</h1> <p>Subtitle</p> <imgsrc="a.jpg"> <div> <ahref="example.html">Goto</a> </div> Baretext <br> <!--thisishtmlcomment--> <p>Bottom</p> </div>
1child::*
节点的所有子元素,如//div[@class="post-content"]/*,结果:
<h1>Title</h1> <p>Subtitle</p> <imgsrc="a.jpg"> <div> <ahref="example.html">Goto</a> </div> <br> <p>Bottom</p>
可以看到,这里只选择了有标签名的节点,不在标签内的Baretext和注释都被过滤了。
2child::text()
节点的所有文本,如//div[@class="post-content"]/text(),结果:
Baretext
3child::node()
节点下的所有内容,不论是标签还是文本还是其他,//div[@class="post-content"]/node(),结果:
<h1>Title</h1> <p>Subtitle</p> <imgsrc="a.jpg"> <div> <ahref="example.html">Goto</a> </div> Baretext <br> <!--thisishtmlcomment--> <p>Bottom</p>
原样输出了其下的所有内容。