1

I have this apparently basic code:

$docSrc = 
    '<?xml version="1.0" encoding="UTF-8" ?>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head><title /></head>
    <body><p>Test paragraph.</p></body>
    </html>';
$domDoc = new DOMDocument();
$domDoc->loadXML($docSrc);
$xpath  = new DOMXPath($domDoc);
$nodeList = $xpath->query('//p');
$this->assertTrue($nodeList->length == 1);

It should succeed, but fails miserably, the length is 0. I have been looking for a solution all day, but to no avail.


Wow thanks! It works! Unfortunately, my original code:

$query = new Zend_Dom_Query($docSrc);
$xpathQuery = '//p';
$result = $query->queryXpath($xpathQuery);
$this->assertTrue($result->count() == 1);

thinks it is an XML and perform loadXML. Do you have any idea why this happens?


Ok I found the culprit somewhere in the Zend Library:

if ('<' . '?xml' == substr(trim($document), 0, 5)) {
    return $this->setDocumentXml($document, $encoding);
}

I wonder if this is correct since it IS an XML document, but load XML does not work.


Ok I’m doing some research. Apparently, the problem has something to do with namespaces...

gurghet
  • 7,591
  • 4
  • 36
  • 63
  • 1
    Unrelated suggestion: using the more specific assertions such as `assertEquals()` will produce more informative failure messages. The above becomes `self::assertEquals(1, $result->count());` so a failure will say, "Expected 1 but was 3" rather than "Expected true but was false." – David Harkness May 14 '11 at 00:19

2 Answers2

2

Change line 2 to:

$domDoc->loadHTML($docSrc);
Ashley
  • 5,939
  • 9
  • 39
  • 82
0

There IS a bug, workaround is:

    $docSrc = 
        '<?xml version="1.0" encoding="UTF-8" ?>
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head><title /></head>
        <body><p>Test paragraph.</p></body>
        </html>';
    $domDoc = new DOMDocument();
    $domDoc->loadXML($docSrc);
    $xpath  = new DOMXPath($domDoc);
    $xpath->registerNamespace('xhtml', 'http://www.w3.org/1999/xhtml');
    $nodeList = $xpath->query('//xhtml:p');
    $this->assertTrue($nodeList->length == 1);
gurghet
  • 7,591
  • 4
  • 36
  • 63