开发者

How to build an Xpath query in objective-c to get every TR except the first?

开发者 https://www.devze.com 2023-02-14 05:01 出处:网络
Currently the html I\'m parsing looks something like this NSString* markup = [NSString stringWithFormat:@\"

Currently the html I'm parsing looks something like this

NSString* markup = [NSString stringWithFormat:@"
<html>
    <head>
        <title>hi</title>
    </开发者_StackOverflow社区head>
    <body>
        <table id='ctl00_ContentPlaceHolder1_dgAppointmentSearchResult'>
            <tr>
                <th>fail</th>
            </tr>
            <tr>
                <td>1</td>
                <td>2</td>
            </tr>
            <tr>
                <td>3</td>
                <td>4</td>
            </tr>
        </table>
    </body>
</html>"];

Next I'm attempting to get each TR (so I can reach each #2 TD inside each TR) except the first TR has a TH inside it so I want to avoid this.

So far I have the following but notice it only pulls a specific TR / TD combo .. not each of them :(

NSData *data = [markup dataUsingEncoding:NSUTF8StringEncoding];
TFHpple* xpathParser = [[TFHpple alloc] initWithHTMLData:data];
NSArray *elements  = [xpathParser search:@"
//table[
   @id='ctl00_ContentPlaceHolder1_dgAppointmentSearchResult'
]/tr[2]/td[2]
"];
TFHppleElement *element = [elements objectAtIndex:0];
NSString *content = [element content];  
[xpathParser release];
[data release];

Any help for an Xpath newbie?


You can use either:

//table[@id='ctl00_ContentPlaceHolder1_dgAppointmentSearchResult']/tr
    [position() > 1]/td[2]

or even more semantic:

//table[@id='ctl00_ContentPlaceHolder1_dgAppointmentSearchResult']/tr
    [not(th)]/td[2]
0

精彩评论

暂无评论...
验证码 换一张
取 消