简体   繁体   English

PHP - 根据在线DTD文件验证XML

[英]PHP - Validate an XML against an online DTD file

I have a xml file and I would like to validate it with a DTD. 我有一个xml文件,我想用DTD验证它。

For that, I included the DTD link in the XML: 为此,我在XML中包含了DTD链接:

<!DOCTYPE article SYSTEM "http://dtd.nlm.nih.gov/1.1/journalpublishing.dtd">

And then did: 然后做了:

$dom = new DOMDocument();
$dom->loadHTML($xml);

if ($dom->validate()) {
    echo "This document is valid!\n";exit;
}
else {
    var_dump("Not OK");exit;
}

The problem is that I am getting this warning message: 问题是我收到此警告消息:

Warning: DOMDocument::validate(http://www.w3.org/TR/REC-html40/loose.dtd): failed to open stream: HTTP request failed! HTTP/1.0 500 Server Error

Any idea? 任何的想法? Thank you. 谢谢。

You are using the wrong method to load the XML. 您使用错误的方法加载XML。

Use load to load an XML file or loadXML to load an XML string. 使用load加载XML文件或loadXML加载XML字符串。 Use loadHTMLFile to load an HTML file and loadHTML to load HTML content. 使用loadHTMLFile加载HTML文件,使用loadHTML加载HTML内容。

Using one of the HTML methods will trigger libxml's HTML parser module , which is 使用其中一种HTML方法将触发libxml的HTML解析器模块 ,即

an HTML 4.0 non-verifying parser with API compatible with the XML parser ones. 一个HTML 4.0非验证解析器,其API与XML解析器兼容。 It should be able to parse "real world" HTML, even if severely broken from a specification point of view. 它应该能够解析“真实世界”的HTML,即使从规范的角度严重破坏。

The HTML Parser module will always use HTML4 Transitional as the DTD, as well as parsing the document with lenient error handling and attempting to auto correct things, for instance, by adding an HTML skeleton to partials, etc. HTML Parser模块将始终使用HTML4 Transitional作为DTD,以及使用宽松错误处理解析文档并尝试自动更正内容,例如,通过向部分内容添加HTML骨架等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM