문제

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

도움이 되었습니까?

해결책

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top