Re: XPath query not working as expected



On Jan 24, 11:15 am, Michael Fesser <neti...@xxxxxx> wrote:
.oO(yawnmoth)



<?php
$xml = new DOMDocument();
$xml->loadHTML('<html><body><font color="blue"><b>test</b></font></
body></html>');
$xpath = new DOMXPath($xml);

//$result = $xpath->query('//descendant::font[@color=blue]');
//$result = $xpath->query('//font[@color=blue]');
$result = $xpath->query('//font[0]');

echo "length = " . $result->length . '<br />';
for ($i = 0; $i < $result->length; $i++)
{
   echo $result->item($i)->textContent . '<br />';
   echo $result->item($i)->nodeValue . '<br />';
}
?>

Why is the length being returned 0?  Shouldn't it be 1?

Counting nodes starts with 1, so this query

//font[1]

should work as expected.

Micha

Nice catch. The problem with the other two, incidentally, (the two
that are commented out) is that I wasn't encapsulating the attribute
value in quotes, be they single or double .
.