2017-09-11 10 views
3

は、私は以下の持って言う:私は<h2>Preparing</h2><p></p>タグのすべてのテキストを照会取得しようとしているXPathで特定のクラスに到達したときにクエリを停止する方法はありますか?

<div class="data"> 
    <h2 class="entry-contentH2">Preparation</h2> 
    <h2>Airplanes</h2> 
    <ul> 
     <li><strong>3 large</strong> wings</li> 
     <li><strong>2</strong>doors</li> 
    </ul> 
    <h2>Car</h2> 
    <ul> 
     <li><strong>4</strong> doors</li> 
     <li><strong>1 cup</strong> holder</li> 
    </ul> 
    <h2 class="stopHeader">Execution</h2> 
    <h2>Motorcycles</h2> 
    <ul> 
     <li>Easy to learn</li> 
    </ul> 
</div> 

、私はそれがstopHeaderクラス前の最後の<p></p>で停止します。

これは私が思い付いたコードである:

//h2[contains(.,"Preparation")]/following-sibling::h2/text()[not(preceding::h2[@class="stopHeader"])] 

#and also 
//h2[contains(.,"Preparation")]/following-sibling::h2/text()[not(preceding::h2[contains(., "Execution")])] 
+0

これは動作するようです。間違った出力を得ましたか? – Andersson

+0

何らかの理由で、stopHeaderの後ろのタグを含め、すべてのpタグを照会しています。 – Craig

+0

'[@ class =" stopHeader "]'を '[@ class =" stopheader "]'に置き換えるとどうなるでしょうか? XPathは大文字と小文字を区別します – Andersson

答えて

2

は、所望の出力を得るためのXPathの下試し:

//h2[.="Preparation"]/following-sibling::h2[./following-sibling::h2[.="Execution"]]/text() 

これは、各ヘッダ(h2)のテキストコンテンツを返す必要があり"Preparation""Execution"

+0

アンダーソンさん、ありがとうございました。しかし、私はここで何が起こったのか理解するつもりです。 – Craig

0

このxpathを試してください。

//h2[text()='Preparation']/following::h2[not(@class='stopHeader')]/text() 
+0

こんにちは、この回答をお試しください。あなたのフィードバックを提供してください。 –

関連する問題