2010-07-01 91 views
2

这个问题可能有点特殊,但测试程序即时通讯使用XPath来查找我需要的HTML数据。这段HTML(在这里找到)就是我试图解析的东西。XPath文本搜索/兄弟选择

<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">1. </td>”中的特定文本找到节点,然后找到该节点“<td align="left" nowrap>”的同胞,然后找到该同胞的子节点“<a href="/stats/individual_stats_player.jsp?c_id=sf&playerID=467055" class="textSm">P Sandoval</a>”以获得期望的结果。我想知道我可以用什么样的查询来找到它。很感谢任何形式的帮助。

+0

好问题(+1)。查看我的答案,了解选择所需节点的单个XPath表达式。 – 2010-07-01 15:42:57

回答

1

使用

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

这假定该上下文(当前节点)针对其上面的的XPath表达式,是table母体。

+0

完美工作 – user380776 2010-07-01 19:24:02

0
//tr[td = "1"]/td[2]/a 

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

+0

感谢您的回复,但是当我做了// // table [@ border ='0'and @ cellspacing ='0'and @ cellpadding ='0']/tr [td ='1']/td [2]/a“我的查询返回没有任何我怀疑这是因为我们使用的1缺乏一段时间,所以当我尝试”//表[@ border ='0'和@ cellspacing ='0'和@ cellpadding ='0']/tr [td ='1。']/td [2]/a“我的程序崩溃了(就像我通常输入无效的xpath命令一样)。 – user380776 2010-07-01 07:28:58

+0

http://stackoverflow.com/questions/247135/using-xpath-to-search-text-containing- – Sjoerd 2010-07-01 07:37:25