I have following code:
<?php
libxml_use_internal_errors(true);
$dom = new DomDocument;
$dom->loadHTMLFile('http://www.next.co.uk/x532328s4');
$xpath = new DomXPath($dom);
$nodes_working = $xpath->query("//html/body/section/section/div/div/div/section/article/section/div/div/h1");
$nodes_not_working = $xpath->query("//html/body/section/section/div/div[2]/div/section[2]/article/section[2]/div[2]/div/h1");
echo '<pre>';
print_r($nodes_working);
echo '</pre>';
foreach ($nodes_working as $i => $node) {
echo "Node($i): ", $node->nodeValue, "\n";
}
echo '<pre>';
print_r($nodes_not_working);
echo '</pre>';
foreach ($nodes_not_working as $i => $node) {
echo "Node($i): ", $node->nodeValue, "\n";
}
?>
Now, the problem is that the path given in $nodes_working is not really correct although it grabs some data sometimes.
On the other hand, the second path which is really correct given in $nodes_not_working doesn't grab anything because it consists numbers which precise which element is the right one if there are more than one 'pararel' elements. So it seems the parser doesn't know what to do when encounter the numeric values there.
My question is: how can I catch the right data in PHP using Xpaths in a format given in $nodes_not_working ?