XPath in PHPUnit testing with Zend
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->queryXpat开发者_StackOverflow社区h($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...
Change line 2 to:
$domDoc->loadHTML($docSrc);
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);
精彩评论