combining XPATH axes (preceding-sibling & followin

2020-07-06 02:20发布

问题:

Say I have the following UL:

<ul>
  <li>barry</li>
  <li>bob</li>
  <li>carl</li>
  <li>dave</li>
  <li>roger</li>
  <li>steve</li>
</ul>

I need to grab all the LIs between bob & roger. I can grab everything after bob with //ul/li[contains(.,"bob")]/following-sibling::li, and I can grab everything before roger with //ul/li[contains(.,"roger")]/preceding-sibling::li. The problem is when I try to combine the two, I end up getting extra results.

For example, //ul/li[contains(.,"bob")]/following-sibling::li[contains(.,"roger")]/preceding-sibling::li will of course get everything before roger, instead of ignoring the items before bob.

Is there a way to chain these two xpaths together?

回答1:

Try:

/ul/li[preceding-sibling::li='bob' and following-sibling::li='roger']


标签: xpath axes