0

2 つの HTML コメント間でコンテンツを選択しようとしていますが、うまくいきません (「 2 つの HTML コメント間で XPath を選択するには?」を参照)。同じ行に新しいコメントがある場合に問題があるようです。

私の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 '])]

すなわち:

//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 に答える