A xml document can be well formed and valid.
well-formed means things like <a><b></b></a> and not <a><b></a></b> and so on. DOMDocument::load() prints warnings and/or ignores parts or the whole document if it isn't well-formed.
DOMDocument::validate() tests the documents against a document type definition (dtd), a "description" of how this kind of document has to be.
e.g. xhtml, html as xml document, has a dtd at
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd
Code: Select all
<?php
$xml = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
</head>
<body>
<p>xyz</p>
</body>
</html>';
echo "loading document\n";
$doc = DOMDocument::loadxml($xml);
echo "validating document\n";
echo $doc->validate() ? 'valid':'invalid';
The xml sring is well-formed but it's not valid "xhtml 1.1. strict", because the dtd requires the head element to have a title element.
loading document
validating document
Warning: DOMDocument::validate(): Element head content does not follow the DTD, expecting ((script | style | meta | link | object)* , ((title , (script | style | meta | link | object)* , (base , (script | style | meta | link | object)*)?) | (base , (script | style | meta | link | object)* , title , (script | style | meta | link | object)*))), got () in /home/Volker/schnickschnack/test.php on line 15
invalid