views:

21

answers:

1

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.&nbsp;</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>&nbsp;</td> 

    <td> 
        <table border="0" cellspacing="0" cellpadding="0"> 
            <tr> 
                <td class="textSm" align="right">1.&nbsp;</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.

+1  A: 

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.

Dimitre Novatchev
Thank you for your answer, but I have a question what node will this take me to exactly, when I use my program to run this it returns nothing. Does this just take me to the <tr> node which is the parent of all this code, or does it take me to the 2nd element of <tr> which is <td> </td>. What code would I need to get to the statistics specifically ... /ancestor::tr[4]? Thanks :)
@user380776 : This will take you to the node you designated as the wanted one: `<tr> <! The node I need to back out into > `. It is the 2nd `tr` ancestor of the `<td class="textSm" align="right">1. </td>`.
Dimitre Novatchev
Thank you very much for your help, but I still have not been able to get this to work. Every time I try to call '.../ancestor::tr[4]' to pull up a statistic my parser crashes out. Why would '.../ancestor::tr[2]' take us to the '<tr>' node directly above '<td class="textSm" align="right">1. </td>'? Thank you again for your help.