Re: XPath query not working as expected



..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
.



Relevant Pages

  • Re: Simple xpath query with default namespace
    ... query (a prefix that's no actually in the xml) in order for the library ... whether it's the default namespace. ... then when you have your XPath query you will need to tell XPath which Account ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: SQLXML Type Conversion Issue
    ... XPath 1.0 only supports double arithmetic according to the standard. ... the offending predicate) or you need to write the FOR XML ... > found that SQLXML is not returning any data for certain XPath queries, ... > The resulting query has the following where clauses: ...
    (microsoft.public.sqlserver.xml)
  • Re: XPath query not working as expected
    ... $xml = new DOMDocument; ... $xpath = new DOMXPath; ... Counting nodes starts with 1, so this query ...
    (comp.lang.php)
  • Struggling with simple DOM/XPath usage...
    ... // $xml has the entire XML document in it; I included it for reference at ... $xpath = new DOMXPath; ... which tells me that my XPath query must be working ... I'd rather do everything using DOM at this point, ...
    (alt.php)
  • Struggling with simple DOM/XPath usage...
    ... // $xml has the entire XML document in it; I included it for reference at ... $xpath = new DOMXPath; ... which tells me that my XPath query must be working ... I'd rather do everything using DOM at this point, ...
    (comp.lang.php)