What’s the equivalent XPath for a CSS selector containing a class, child and adjacent sibling selector?
I've got this:
li.current+li>a
However I have only a basic knowledge of XPat开发者_开发技巧h. I need it for the AutoPager Firefox extension.
I'd also like to get the first match only.
If your li
belongs to many classes, then...
//li[contains(concat(' ',normalize-space(@class),' '),' current ')]/following-sibling::*[1]/self::li/a
here's the same thing broken up over many lines for clarity:
//li[contains(
concat(' ',normalize-space(@class),' '),
' current '
)
]/following-sibling::*[1]/self::li/a
citation: http://pivotallabs.com/users/alex/blog/articles/427-xpath-css-class-matching
I tested it with the Online XPath 2 evaluator using the following XML:
<html>
<head>
<title>
Consume usage app support thread #2 - Apps - iPhone - Whirlpool Forums
</title>
</head>
<body>
<div id="root">
<ul id="top_pagination" class="pagination ">
<li class="first">
<a href="/forum-replies.cfm?t=1543353">
first: 5 months ago
</a>
</li>
<li class=" ">
<a href="/forum-replies.cfm?t=1543353">
1
</a>
</li>
<li class=" ">
<a href="/forum-replies.cfm?t=1543353&p=2">
2
</a>
</li>
<li class=" ">
<a href="/forum-replies.cfm?t=1543353&p=3">
3
</a>
</li>
<li class=" ">
<a href="/forum-replies.cfm?t=1543353&p=4">
4
</a>
</li>
<li class=" ">
<a href="/forum-replies.cfm?t=1543353&p=5">
5
</a>
</li>
<li class=" ">
<a href="/forum-replies.cfm?t=1543353&p=6">
6
</a>
</li>
<li class=" ">
<a href="/forum-replies.cfm?t=1543353&p=7">
7
</a>
</li>
<li class=" ">
<a href="/forum-replies.cfm?t=1543353&p=8">
8
</a>
</li>
<li class=" ">
<a href="/forum-replies.cfm?t=1543353&p=9">
9
</a>
</li>
<li class=" ">
<a href="/forum-replies.cfm?t=1543353&p=10">
10
</a>
</li>
<li class=" ">
<a href="/forum-replies.cfm?t=1543353&p=11">
11
</a>
</li>
<li class=" ">
<a href="/forum-replies.cfm?t=1543353&p=12">
12
</a>
</li>
<li class=" ">
<a href="/forum-replies.cfm?t=1543353&p=13">
13
</a>
</li>
<li class=" ">
<a href="/forum-replies.cfm?t=1543353&p=14">
14
</a>
</li>
<li class=" ">
<a href="/forum-replies.cfm?t=1543353&p=15">
15
</a>
</li>
<li class=" ">
<a href="/forum-replies.cfm?t=1543353&p=16">
16
</a>
</li>
<li class=" ">
<a href="/forum-replies.cfm?t=1543353&p=17">
17
</a>
</li>
<li class=" ">
<a href="/forum-replies.cfm?t=1543353&p=18">
18
</a>
</li>
<li class=" ">
<a href="/forum-replies.cfm?t=1543353&p=19">
19
</a>
</li>
<li class=" ">
<a href="/forum-replies.cfm?t=1543353&p=20">
20
</a>
</li>
<li class=" ">
<a href="/forum-replies.cfm?t=1543353&p=21">
21
</a>
</li>
<li class=" ">
<a href="/forum-replies.cfm?t=1543353&p=22">
22
</a>
</li>
<li class=" ">
<a href="/forum-replies.cfm?t=1543353&p=23">
23
</a>
</li>
<li class=" ">
<a href="/forum-replies.cfm?t=1543353&p=24">
24
</a>
</li>
<li class=" ">
<a href="/forum-replies.cfm?t=1543353&p=25">
25
</a>
</li>
<li class="current ">
<a href="/forum-replies.cfm?t=1543353&p=26">
26
</a>
</li>
<li class=" ">
<a href="/forum-replies.cfm?t=1543353&p=27">
27
</a>
</li>
<li class="last">
<a href="/forum-replies.cfm?t=1543353&p=-1&#bottom">
last: 2 hours ago
</a>
</li>
</ul>
</div>
</body>
</html>
You should note that the page you linked to as your input - http://forums.whirlpool.net.au/forum-replies.cfm?t=1543353&p=26 - is HTML, not XML:
- its doctype declaration is for HTML 4.01
- the document contains unclosed tags like
<br>
which would have to be<br />
... so you may have problems using it as input for XPath evaluation.
li.current + li > a
maps to:
//li[@class='current']/following-sibling::*[1]/self::li/a
If you want to select the first a
child element, your CSS selector should be:
li.current + li > a:first-child
which maps to:
//li[@class='current']/following-sibling::*[1]/self::li/a[1]
精彩评论