【发布时间】: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