Question

I have a xml file and I would like to validate it with a DTD.

For that, I included the DTD link in the XML:

<!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.

Was it helpful?

Solution

You are using the wrong method to load the XML.

Use load to load an XML file or loadXML to load an XML string. Use loadHTMLFile to load an HTML file and loadHTML to load HTML content.

Using one of the HTML methods will trigger libxml's HTML parser module, which is

an HTML 4.0 non-verifying parser with API compatible with the XML parser ones. It should be able to parse "real world" HTML, even if severely broken from a specification point of view.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top