【问题标题】:Selecting the next link using XPath使用 XPath 选择下一个链接
【发布时间】:2011-01-13 00:41:53
【问题描述】:

我必须编写一个 XPath 表达式来获取下面 html 中锚标记的 href 属性,该属性紧跟在标记为“当前页面”的标记之后(在示例中为 #notimportant/2)。

<dd>
    <a href="#notimportant/1" class="current-page">1</a>
    <a href="#notimportant/2">2</a>
    <a href="#notimportant/3">3</a>
    <a href="#notimportant/4">4</a>
    <!-- EDIT: Do not return the next sibling if @href ends with /last -->
    <a href="#notimportant/last">last</a>
</dd>

我想过从 //a[@class='current-page']/../next-sibling-of-first-node/@href 之类的东西开始,但我被困在这里......

谁能帮我解决这个问题?我搜索了一下,但 XPath 不是我最喜欢的技能(不,我不能使用 jQuery。它不是 web 应用程序)。

【问题讨论】:

  • 什么意思返回空?如果没有后续兄弟,一般会返回null。
  • 对不起,你可以看到我是一个 XPath 菜鸟。如果它的href不以“/last”结尾,我只想返回下一个元素。否则我希望表达式返回 null。

标签: xml parsing xpath html-parsing


【解决方案1】:
//a[@class='current-page']/following-sibling::a[1]

【讨论】:

  • 伙计们,你们对我来说太快了。 :-P
  • 谢谢大家。那是超级快。我已经编辑了我的问题,您介意查看答案吗?
猜你喜欢
  • 2020-09-06
  • 1970-01-01
  • 2013-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多