2

I am trying to extract data from a table using...

 $tags = $xpath->query('//tr[@valign="top"]/td/span[@style="background-color:#cccccc;font-weight:bold;"]'); 

on a row that looks like...

 <tr valign="top"> <td style="background-color:#cccccc;"><span style="background-color:#cccccc;font-weight:bold;">Some Company</span> 

But its not retrieving the data, it works using the DOM inspector in firebug... using the xpath...

 $tags = $xpath->query('//tr[@valign="top"]/td/span'); 

works but it pulls other data not wanted. Any suggestions?

1 Answer 1

1

Is it possible there is some ordering/formatting issue with those style properties? Try the following:

//tr[@valign="top"]/td/span[contains(@style, 'background-color:#cccccc') and contains(@style, 'font-weight:bold')] 

You could also try selecting based on the contents of the cell (assuming the value is unique):

//tr[@valign="top"]/td/span[.='Some Company'] 

Or (less restrictive):

//tr[@valign="top"]/td/span[contains(., 'Some Company')] 
Sign up to request clarification or add additional context in comments.

5 Comments

The value is not unique and it is a list, so I need that node of each table row. I tried your first example to no avail. Do single or double quotes matter? PHP wont let me use the ' without a \' in front of it. I cant see any ordering/formatting issues, as I copied the above row as it shows on the page source.
@savagenoob - Copied from the actual source, or from what Firebug showed you?
@savagenoob - Also, have you tried selecting by position? //tr[@valign="top"]/td/span[1]
@savagenoob - If you provide a more complete sample input, I'll try to provide a more accurate expression.
I realized that I used $html = @mb_convert_encoding($html, 'HTML-ENTITIES', 'utf-8'); when doing the cURL to get the page which changed the layout. Thanks for the help.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.