XPath Ancestor Axis Multiple Parents
So I am writing a mac application that parses SF Giants baseball statistics using xpath. In my situation I have a specific number Identifier of a person <td class="textSm" align="right">1. </td>
(in my application a person could search find a stat based on a number input) but from there I need to back out from that spot in the HTML, and go to the first node, where I can get to the actual statistics towards the bottom of the HTML.
<tr> <! The node I need to back out into >
<td><img src="/images/trans.gif" width="1" height="1" 开发者_运维问答border="0" /></td>
<td> </td>
<td>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="textSm" align="right">1. </td> <! Number Identifier here >
<td align="left" nowrap>
<a href="/stats/individual_stats_player.jsp?c_id=sf&playerID=467055" class="textSm">P Sandoval</a>
</td>
</tr>
</table>
</td>
<td align="center">SF</td> <! list of statistics starts here >
<td align="center">3B</td>
<td align="center">83</td>
<td bgcolor="#CCCCCC" align="center">326</td>
<td align="center">41</td>
<td align="center">88</td>
<td align="center">18</td>
<td align="center">2</td>
<td align="center">6</td>
<td align="center">34</td>
<td align="center">128</td>
<td align="center">27</td>
<td align="center">45</td>
<td align="center">2</td>
<td align="center">2</td>
<td align="center">.324</td>
<td align="center">.393</td>
<td align="center">.270</td>
</tr>
So far I have this //table[@border='0' and @cellspacing='0' and @cellpadding='0']/tr/td[starts-with(., '1.')]
which will find my number identifier. What I cant get working is backing out all the way to the parent node <tr>
where the statistics are located in. My best guess is to do //table[@border='0' and @cellspacing='0' and @cellpadding='0']/tr/td[starts-with(., '1.')]/ancestor::tr/ancestor::table/ancestor::td/ancestor::tr[1(or any other number for the statistic i want]
. Any thanks would be greatly appreciated.
For the XML fragment that is provided, the following XPath expression will locate what seems to be the desired node:
//table[@border='0' and @cellspacing='0' and @cellpadding='0']
/tr/td[starts-with(., '1.')]
/ancestor::tr[2]
This selects the first ancestor <tr>
of the first ancestor <tr>
of the node identified using the XPath expression provided in the question.
精彩评论