Page 1 of 1

[SOLVED] DOMDocument Level 1

Posted: Tue Aug 07, 2007 6:59 am
by dethron
Hello All,

Code: Select all

<?php
	$file = "somexmlfile.xml";

	$dom = new DOMDocument;
	$dom->Load($file);
	$dom->validate();
?>
it prints

Code: Select all

Warning: DOMDocument::validate() [function.DOMDocument-validate]: no DTD found! in /var/www/test/dom.php on line 6
instead of blank page, do you have any idea for the warning?

Posted: Tue Aug 07, 2007 8:00 am
by volka
What do you expect from validate()? What do you want to be validated?

Posted: Tue Aug 07, 2007 8:15 am
by dethron
an XML document 8)

I loaded somexmlfile.xml to validate

Posted: Tue Aug 07, 2007 8:27 am
by dethron
does it expect dom.php should have a DTD?

Posted: Tue Aug 07, 2007 8:32 am
by iknownothing
might be an idea, so validate() knows what its validating standard is.

Posted: Tue Aug 07, 2007 8:47 am
by dethron
iknownothing wrote:might be an idea, so validate() knows what its validating standard is.
:( nope still same error. i guess there is not enough documentation for DOMDocument :(

Posted: Tue Aug 07, 2007 9:19 am
by volka
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

Posted: Tue Aug 07, 2007 9:29 am
by dethron
hmmm ok then, this warning means that the remote XML file that I have loaded no dtd type :) ok then, there is nothing to do with it.