Hi, I have problem with evaluating XPath with query. When I use this in xpath_tester script:
URL: http://www.seznam.cz
XPath: //*[@id='gadget-6']/div/div/h3/span[1]
Works fine, but this:
URL: http://www.seznam.cz
XPath: //*[@id='gadget-1']/div/div/div/div/table/tbody/tr[1]/td/div/div/strong/a
Result: "ERROR 2: No result!", because there are no items in $nodeList
I'm 100% sure XPath is OK. (using XPath checker extension for firefox)
Here is code of xpath_tester.php:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>XPath tester</title>
</head>
<body>
<?php
if (isset($_POST['xpath']) && isset($_POST['url'])) {
$url = $_POST['url'];
$xpath = $_POST['xpath'];
$doc = new DOMDocument();
@$doc->loadHTMLFile($url);
$xp = new DOMXPath($doc);
$nodeList = $xp->query($xpath);
if ($nodeList != FALSE) {
if ($nodeList->length > 0) {
$node = $nodeList->item(0);
echo $node->textContent;
} else {
echo "ERROR 2: No result!";
}
} else {
echo "ERROR 1!";
}
} else {
$url = "";
$xpath = "";
}
echo '
<form action="xpath_tester.php" method="post">
<label for="url">URL</label><input type="text" name="url" size="100" value="'.$url.'" /><br />
<label for="xpath">XPath</label><input type="text" name="xpath" size="100" value="'.$xpath.'" /><br />
<input type="submit" value="OK" />
</form>';
?>
</body>
</html>