包含类,子和相邻兄弟选择器的CSS选择器的等效XPath是什么?

时间:2011-03-17 12:22:23

标签: css xpath css-selectors

我有这个:

li.current+li>a

但是我只掌握了XPath的基本知识。 我需要它用于AutoPager Firefox扩展。

我也想只参加第一场比赛。

2 个答案:

答案 0 :(得分:3)

如果您的li属于许多课程,那么......

//li[contains(concat(' ',normalize-space(@class),' '),' current ')]/following-sibling::*[1]/self::li/a

为了清晰起见,这里有许多分解的相同内容:

//li[contains(
        concat(' ',normalize-space(@class),' '),
        ' current '
     )
]/following-sibling::*[1]/self::li/a

引用:http://pivotallabs.com/users/alex/blog/articles/427-xpath-css-class-matching

我使用以下XML使用Online XPath 2 evaluator对其进行了测试:

<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&amp;p=2">
                        2
                    </a>
                </li>
                <li class=" ">
                    <a href="/forum-replies.cfm?t=1543353&amp;p=3">
                        3
                    </a>
                </li>
                <li class=" ">
                    <a href="/forum-replies.cfm?t=1543353&amp;p=4">
                        4
                    </a>
                </li>
                <li class=" ">
                    <a href="/forum-replies.cfm?t=1543353&amp;p=5">
                        5
                    </a>
                </li>
                <li class=" ">
                    <a href="/forum-replies.cfm?t=1543353&amp;p=6">
                        6
                    </a>
                </li>
                <li class=" ">
                    <a href="/forum-replies.cfm?t=1543353&amp;p=7">
                        7
                    </a>
                </li>
                <li class=" ">
                    <a href="/forum-replies.cfm?t=1543353&amp;p=8">
                        8
                    </a>
                </li>
                <li class=" ">
                    <a href="/forum-replies.cfm?t=1543353&amp;p=9">
                        9
                    </a>
                </li>
                <li class=" ">
                    <a href="/forum-replies.cfm?t=1543353&amp;p=10">
                        10
                    </a>
                </li>
                <li class=" ">
                    <a href="/forum-replies.cfm?t=1543353&amp;p=11">
                        11
                    </a>
                </li>
                <li class=" ">
                    <a href="/forum-replies.cfm?t=1543353&amp;p=12">
                        12
                    </a>
                </li>
                <li class=" ">
                    <a href="/forum-replies.cfm?t=1543353&amp;p=13">
                        13
                    </a>
                </li>
                <li class=" ">
                    <a href="/forum-replies.cfm?t=1543353&amp;p=14">
                        14
                    </a>
                </li>
                <li class=" ">
                    <a href="/forum-replies.cfm?t=1543353&amp;p=15">
                        15
                    </a>
                </li>
                <li class=" ">
                    <a href="/forum-replies.cfm?t=1543353&amp;p=16">
                        16
                    </a>
                </li>
                <li class=" ">
                    <a href="/forum-replies.cfm?t=1543353&amp;p=17">
                        17
                    </a>
                </li>
                <li class=" ">
                    <a href="/forum-replies.cfm?t=1543353&amp;p=18">
                        18
                    </a>
                </li>
                <li class=" ">
                    <a href="/forum-replies.cfm?t=1543353&amp;p=19">
                        19
                    </a>
                </li>
                <li class=" ">
                    <a href="/forum-replies.cfm?t=1543353&amp;p=20">
                        20
                    </a>
                </li>
                <li class=" ">
                    <a href="/forum-replies.cfm?t=1543353&amp;p=21">
                        21
                    </a>
                </li>
                <li class=" ">
                    <a href="/forum-replies.cfm?t=1543353&amp;p=22">
                        22
                    </a>
                </li>
                <li class=" ">
                    <a href="/forum-replies.cfm?t=1543353&amp;p=23">
                        23
                    </a>
                </li>
                <li class=" ">
                    <a href="/forum-replies.cfm?t=1543353&amp;p=24">
                        24
                    </a>
                </li>
                <li class=" ">
                    <a href="/forum-replies.cfm?t=1543353&amp;p=25">
                        25
                    </a>
                </li>
                <li class="current ">
                    <a href="/forum-replies.cfm?t=1543353&amp;p=26">
                        26
                    </a>
                </li>
                <li class=" ">
                    <a href="/forum-replies.cfm?t=1543353&amp;p=27">
                        27
                    </a>
                </li>
                <li class="last">
                    <a href="/forum-replies.cfm?t=1543353&amp;p=-1&amp;#bottom">
                        last: 2 hours ago
                    </a>
                </li>
            </ul>
        </div>
    </body>
</html>

您应该注意,您链接的页面作为输入 - http://forums.whirlpool.net.au/forum-replies.cfm?t=1543353&p=26 - 是HTML,而不是XML:

  • 其doctype声明适用于HTML 4.01
  • 该文档包含<br>等未公开标签,必须为<br />

...因此,您可能无法将其用作XPath评估的输入。

答案 1 :(得分:0)

li.current + li > a映射到:

//li[@class='current']/following-sibling::*[1]/self::li/a

如果要选择第一个a子元素,则CSS选择器应为:

li.current + li > a:first-child

映射到:

//li[@class='current']/following-sibling::*[1]/self::li/a[1]