【问题标题】:XPath Text Search/Sibling SelectionXPath 文本搜索/同级选择
【发布时间】:2010-07-01 06:22:46
【问题描述】:

这个问题可能有点具体,但我正在编写的测试程序使用 XPath 在 HTML 中查找我需要的数据。这段 HTML(在此处找到)是我试图解析的内容。

<table border="0" cellspacing="0" cellpadding="0">
    <tr>
        <td class="textSm" align="right">1.&nbsp;</td> <!-- Location of the number is here -->
        <td align="left" nowrap>
            <a href="/stats/individual_stats_player.jsp?c_id=sf&playerID=467055" class="textSm">P Sandoval</a> <!-- Player location is here of the number is here -->
        </td>
    </tr>
</table>

我的目标是通过使用与他对应的数字来找到他的名字。这需要我通过“td class="textSm" align="right"&gt;1. &lt;/td&gt;”中包含的特定文本找到一个节点,然后找到该节点的兄弟节点“&lt;td align="left" nowrap&gt;”,然后找到该兄弟节点的子节点“&lt;a href="/stats/individual_stats_player.jsp?c_id=sf&amp;playerID=467055" class="textSm"&gt;P Sandoval&lt;/a&gt;”以获得所需的结果。我想知道我可以使用什么样的查询来找到这个。非常感谢任何帮助。

【问题讨论】:

  • 好问题 (+1)。请参阅我对选择所需节点的单个 XPath 表达式的回答。

标签: objective-c xpath hpple


【解决方案1】:

使用

table/tr/td[starts-with(., '1.')]/following-sibling::td/a

这假定计算上述 XPath 表达式所针对的上下文(当前节点)是 table 的父级。

【讨论】:

    【解决方案2】:
    //tr[td = "1"]/td[2]/a
    

    对于所有 TD 等于 '1' 的 TR,从第二个子 TD 给 A 元素。

    【讨论】:

    • 感谢您的回复,但是当我这样做时 "//table[@border='0' and @cellspacing='0' and @cellpadding='0']/tr[td = '1 ']/td[2]/a" 我的查询没有返回任何内容我怀疑这是因为我们使用的 1 缺少句点所以当我尝试 "//table[@border='0' and @cellspacing='0' and @ cellpadding='0']/tr[td = '1.']/td[2]/a" 我的程序崩溃了(就像我输入无效的 xpath 命令时通常那样)。
    猜你喜欢
    • 2015-12-29
    • 2017-03-29
    • 2018-12-06
    • 1970-01-01
    • 1970-01-01
    • 2016-11-08
    • 1970-01-01
    • 2014-01-12
    • 2023-03-17
    相关资源
    最近更新 更多