php – DOMDocument :: validate()问题

前端之家收集整理的这篇文章主要介绍了php – DOMDocument :: validate()问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个很大的问题,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

相关问题:

> How to import XML string in a php DOMDocument ?(已解决)

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

> http://bugs.php.net/bug.php?id=48080

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

> libxml_set_streams_context()
> list of available HTTP context options

例:

$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)
原文链接:https://www.f2er.com/php/240045.html

猜你在找的PHP相关文章