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



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: Parsing XML
    ... For the xml fragment you provide, you can simply use XPATH to query out the ... element and its innerTEXT data. ...
    (microsoft.public.dotnet.languages.vb)
  • 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)