DOMDocument :: validate()问题

时间:2010-10-31 10:46:32

标签: php xml xhtml domdocument

我对PHP DOMDocument :: validate()有一个很大的问题,他似乎系统地询问了DTD。

当我想验证一个XHTML文档as explained here时,这是一个很大的问题。

由于w3.org似乎拒绝来自PHP服务器的所有请求,因此无法使用此方法验证我的文档...

有没有解决方案?

先谢谢

[编辑]以下是一些准确性:

/var/www/test.php:

<?php
$implementation = new DOMImplementation();

$dtd = $implementation->createDocumentType
       (
         'html',                                     // qualifiedName
         '-//W3C//DTD XHTML 1.0 Transitional//EN',   // publicId
         'http://www.w3.org/TR/xhtml1/DTD/xhtml1-'
           .'transitional.dtd'                       // systemId
       );

$document = $implementation->createDocument('', '', $dtd);

$document->validate();

[http://]127.0.0.1/test.php

Warning: DOMDocument::validate(http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden
 in /var/www/test.php on line 14

Warning: DOMDocument::validate(): I/O warning : failed to load external entity "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" in /var/www/test.php on line 14

Warning: DOMDocument::validate(): Could not load the external subset "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" in /var/www/test.php on line 14

相关问题:

1 个答案:

答案 0 :(得分:8)

与评论中指出的一样,DOMDocument::validate有一个Bug / FeatureRequest接受DTD作为字符串:

您可以自己托管DTD并相应地更改systemId,也可以为通过libxml完成​​的任何加载提供自定义流上下文。例如,提供UserAgent将绕过W3C的阻塞。您也可以通过这种方式添加代理。见

示例:

$di = new DOMImplementation;
$dom = $di->createDocument(
    'html',
    'html',
    $di->createDocumentType(
        'html',
        '-//W3C//DTD XHTML 1.0 Transitional//EN',
        'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'
    )
);
$opts = array(
    'http' => array(
        'user_agent' => 'PHP libxml agent',
    )
);
$context = stream_context_create($opts);
libxml_set_streams_context($context);

var_dump($dom->validate());

这将输出

Warning: DOMDocument::validate(): Element html content does not follow the DTD, expecting (head , body), got  

Warning: DOMDocument::validate(): Element html namespace name for default namespace does not match the DTD 

Warning: DOMDocument::validate(): Value for attribute xmlns of html is different from default "http://www.w3.org/1999/xhtml" 

Warning: DOMDocument::validate(): Value for attribute xmlns of html must be "http://www.w3.org/1999/xhtml" 

bool(false)