The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Parsing html with Xpath without simpleXML

I have been wrecking my brain trying to figure out how to use xpath, alone, to parse html code.
A conscious effort was made to learn xpath to the exclusion of simplexml, because; simplexml is quite limited, IMHO based on my research.

You know, if you parsing this exact document, you can make your xpath simpler:

$results = $xpath->query('//ul/li');
// that's it
Now $result is an object of type DOMNodeList, it's not an array but it's iteratable
Now you can do your foreach
foreach($result as $e){
$maker = $e->getElementsByTagName('a')->item(0)->nodeValue;
$cars = $xpath->query('//li', $e);
foreach($cars as $car){
$s .= '<h3>'.$car->item(0)->nodeValue.'</h3>';
}
// now here you can use the value of $maker and $s which is a string made in innder loop
}

I am not sure, but you may not even need to get the ->item(0) in the inner foreach loop,
and should probably use just $car->nodeValue

I have to mention that I have not tested my example, it's just a basic outline on how you should proceed. Just remember that you can iterate over xpath results and each item is an element DOMNode, so you can use methods and properties of DOMNode on items.

A conscious effort was made to learn xpath to the exclusion of simplexml, because; simplexml is quite limited, IMHO based on my research.

Out of curiosity, how did you find SimpleXML limiting? When it comes to XPath, SimpleXML's only real limitation is that it will only return elements (like DOMElement), not text nodes (DOMText) and other nodes.

Here's how I'd do what you're talking about in your first post with SimpleXML: