本文介绍了组合 XPATH 轴(前兄弟和后兄弟)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下 UL:

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

我需要获取 bob & 之间的所有 LI罗杰.我可以用 //ul/li[contains(.,"bob")]/following-sibling::li 在 bob 之后抓取所有东西,我可以用 //在 roger 之前抓取所有东西ul/li[contains(.,"roger")]/preceding-sibling::li.问题是当我尝试将两者结合起来时,我最终会得到额外的结果.

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.

例如,//ul/li[contains(.,"bob")]/following-sibling::li[contains(.,"roger")]/preceding-sibling::li 当然会得到 roger 之前的所有东西,而不是忽略 bob 之前的项目.

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.

有没有办法将这两个 xpath 链接在一起?

Is there a way to chain these two xpaths together?

推荐答案

尝试:

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

这篇关于组合 XPATH 轴(前兄弟和后兄弟)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 07:51