2010-02-04 84 views
3

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

<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应用程序)。

+0

你是什么意思return empty?如果没有下一个兄弟,它通常会返回null。 – 2010-02-04 11:46:11

+0

对不起,你可以看到我是一个XPath noob。 我只想返回下一个元素,如果它的href不以“/ last”结尾的话。否则,我希望表达式返回null。 – Pablo 2010-02-04 12:03:45

回答

8
//a[@class='current-page']/following-sibling::a[1] 
+5

+1,惊人的答案,@Matthew! 9秒=) – 2010-02-04 10:54:13

+1

伙计们,你对我来说太快了。 :-P – Tomalak 2010-02-04 11:10:07

+0

谢谢你们。这非常快。我编辑了我的问题,您是否介意查看答案? – Pablo 2010-02-04 11:22:03