George
George

Reputation: 2110

What's wrong with this XPath query?

I want to make headers list from this url: http://www.2dplay.com/action-games.htm

My query is as given:

 $gamelist = $xpath->query('//div[@id="wrapper"]//div[@id="body_wrap"]//div[@id="content"]//
table[@id="cat_games"]//tbody//tr//td//h2//a');
    foreach($gamelist as $e){
        echo $e->nodeValue;
        echo "<br/>";
    }

It gives no results. If I close the query to table[@id="cat_games"] it gives every txt information in one node. Any help will be greatly appreciated.

Upvotes: 2

Views: 539

Answers (1)

netcoder
netcoder

Reputation: 67715

You must know that the id attribute for an element must be unique, according to W3C XHTML 1.0 C.8 section. XHTML 1.0 is a reformulation of HTML 4 in XML 1.0, therefore the HTML4 7.5.2 definition applies here too.

Since the document you are parsing is declared as XHTML 1.0, you do not need to provide the full path to the element you want because the table element has an id attribute. You can resolve this element directly instead:

//table[@id="cat_games"]/tr/td/h2/a

If you fear the structure of the table may change (i.e.: a <tbody> tag may be added eventually), you can also make a more generic query:

//table[@id="cat_games"]//h2/a

Upvotes: 4

Related Questions