[SOLVED] DOMDocument Level 1

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

[SOLVED] DOMDocument Level 1

Post 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?
Last edited by dethron on Tue Aug 07, 2007 9:29 am, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

What do you expect from validate()? What do you want to be validated?
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

an XML document 8)

I loaded somexmlfile.xml to validate
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

does it expect dom.php should have a DTD?
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post by iknownothing »

might be an idea, so validate() knows what its validating standard is.
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post 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 :(
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post 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.
Post Reply