note 89893 added to domdocument.schemavalidate
| From: | NetPanther at osu1 dot php dot net | Date: | Fri, 27 Mar 2009 09:42:03 +0000 |
| Subject: | note 89893 added to domdocument.schemavalidate | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-152516@lists.php.net to get a copy of this message | ||
Initial situation:
- Debian Lenny
- Apache 2 with PHP 5.2.6
- libxml 2.6.32
Problem: While trying to validate a manually created (!) DOMDocument against an existing XML Schema,
I ran into a warning like the one below. Validation fails, even though the document IS valid and the
command line tool xmllint confirms this (even with libxml 2.6.32, so this must be a problem with
DOM). The validation works fine with libxml 2.7.3.
Warning: DOMDocument::schemaValidate() [domdocument.schemavalidate]: Element
'YourRootElementName': No matching global declaration available for the validation root.
in /var/www/YourFile.php on line X
Solution: As libxml 2.7.3 is not provided for Debian Lenny yet and this problem seems to be caused
by DOM (s.o.), I currently use the following workaround on my machines. DOM obviously has some
namespace problems with documents, that have been created manually (i.e. they were not loaded from a
file).
So my workaround is saving the DOMDocument temporarily, re-loading it and then validating the
temporary DOMDocument. Strangely enough the validation of the same document (= same content) now
works. Sure, creating a temporary file is not a nice solution, but unless this bug is fixed, this
workaround should do just fine.
<?php
// Works with libxml 2.7.3 and higher.
public function isValid()
{
return $this->dom->schemaValidate('schema.xsd');
}
// Workaround for prior libxml versions, e.g. 2.6.32.
public function isValid()
{
// Create temporary file and save manually created DOMDocument.
$tempFile = time() . '-' . rand() . '-document.tmp';
$this->dom->save($tempFile);
// Create temporary DOMDocument and re-load content from file.
$tempDom = new DOMDocument();
$tempDom->load($tempFile);
// Delete temporary file.
if (is_file($tempFile))
{
unlink($tempFile);
}
// Validate temporary DOMDocument.
return $tempDom->schemaValidate('schema.xsd');
}
?>
----
Server IP: 217.13.201.10
Probable Submitter: 212.37.47.66
----
Manual Page -- http://www.php.net/manual/en/domdocument.schemavalidate.php
Edit -- https://master.php.net/note/edit/89893
Del: integrated -- https://master.php.net/note/delete/89893/integrated
Del: useless -- https://master.php.net/note/delete/89893/useless
Del: bad code -- https://master.php.net/note/delete/89893/bad+code
Del: spam -- https://master.php.net/note/delete/89893/spam
Del: non-english -- https://master.php.net/note/delete/89893/non-english
Del: in docs -- https://master.php.net/note/delete/89893/in+docs
Del: other reasons-- https://master.php.net/note/delete/89893
Reject -- https://master.php.net/note/reject/89893
Search -- https://master.php.net/manage/user-notes.php