简体   繁体   中英

Regex vs. xpath when grabbing two following blocks in html

I am trying to grab the two blocks of divs in a html file.

<...>
  <div id="test">
  ...
  </div>
  <div id="test2">
  ...
  </div>
  ...
</...>

This is just a very simple example. Basically I want to find two blocks that I could as separate find with an xpath like "./div[@id="test"]" and "./div[@id="test2"]".
However I want to catch both divs together and want to make sure that the two blocks are right next to each other. Can this be done in XPath or should I better use a regular expression?

Thanks J.

To find divs you want use such Xpath

//div[@id="test"][following::*[1][name() = "div" and @id="test2"]]

It finds div with @id="test" and the 1st element after is div with @id="test2"

Unfortunately we can't use variable for temporary result, so we need to write it twice to take both divs

//div[@id="test"][following::*[1][name() = "div" and @id="test2"]] | 
//div[@id="test"][following::*[1][name() = "div" and @id="test2"]]/following::*[1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM