DOMDocument :: validate()问题 [英] DOMDocument::validate() problem
问题描述
我的PHP DOMDocument :: validate()有一个大问题,他似乎是在系统地询问DTD.
I have a big problem with the PHP DOMDocument::validate() who seems to ask the DTD systematically.
It is a big problem when I whant to validate, for example, an XHTML document as explained here.
由于w3.org似乎拒绝了来自PHP服务器的所有请求,因此无法使用此方法来验证我的文档...
As w3.org seems to reject all request from a PHP server, it's impossible to validate my document with this method...
有什么解决办法吗?
多谢
这是一些精度:
/var/www/test.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
? (solved)
推荐答案
有一个Bug/FeatureRequest,用于DOMDocument::validate
接受DTD作为字符串:
Like pointed out in the comments, there is a Bug/FeatureRequest for DOMDocument::validate
to accept the DTD as a string:
您可以自己托管DTD并相应地更改systemId
,也可以为通过libxml完成的任何加载提供自定义流上下文.例如,提供UserAgent可以解决W3C的阻塞问题.您也可以通过这种方式添加代理.参见
You could host the DTD yourself and change the systemId
accordingly or you can provide a custom stream context to any loading done through libxml. For instance, providing a UserAgent will get around the W3C's blocking. You could also add proxy this way. See
示例:
$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)
这篇关于DOMDocument :: validate()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!