在两个HTML注释之间选择的XPath不起作用? [英] XPath to select between two HTML comments is not working?

查看:142
本文介绍了在两个HTML注释之间选择的XPath不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在两个HTML注释之间选择一些内容,但是在正确处理它时遇到了一些麻烦(如"

I'm trying to select some content between two HTML comments, but having some trouble getting it right (as seen in "XPath to select between two HTML comments?"). There seems to be a problem when new comments that are on the same line.

我的HTML:

<html>
 ........
 <!-- begin content -->
 <div>some text</div>
 <div>
   <p>Some more elements</p>
 </div>
 <!-- end content --><!-- begin content -->
 <div>more text</div>
 <!-- end content -->
 .......
</html>

我使用:

doc.xpath("//node()[preceding-sibling::comment()[. = ' begin content ']]
          [following-sibling::comment()[. = ' end content ']]")

结果:

<div>some text</div>
<div>
  <p>Some more elements</p>
</div>
<!-- end content --><!-- begin content -->
<div>more text</div>

我想要得到的是什么

<div>some text</div>
<div>
  <p>Some more elements</p>
</div>

推荐答案

如果您对第一条评论感兴趣,则可以从查找第一条评论开始:

If you are interested in the first pair of comments, you could start with looking for the first comment:

//comment()[.=' begin content ']/following::*[not(preceding::comment()[.=' end content '])]

即:

//comment()[1][.=' begin content ']           <-- look for first suitable comment
    /following::*                             <-- take all following nodes
         [not(preceding::comment()[.=' end content '])] <-- satisfying condition there is no preceding "end comment"

这篇关于在两个HTML注释之间选择的XPath不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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