0

我正在尝试在两个 HTML 注释之间选择一些内容,但是在正确处理它时遇到了一些问题(如“ XPath to select between two HTML comments? ”中所示)。当新评论在同一行时似乎有问题。

我的 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>
4

1 回答 1

1

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

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

IE:

//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"
于 2013-10-29T16:42:17.100 回答