DOMDocument :: validate()问题

Pas*_*Qyy 7 php xml xhtml domdocument

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

当我想验证例如这里解释的XHTML文档时,这是一个大问题.

由于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();
Run Code Online (Sandbox Code Playgroud)

[ 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
Run Code Online (Sandbox Code Playgroud)

相关问题:

Gor*_*don 8

就像在评论中指出的那样,有一个Bug/FeatureRequest用于DOMDocument::validate接受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());
Run Code Online (Sandbox Code Playgroud)

这会输出

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)
Run Code Online (Sandbox Code Playgroud)