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
public function isValid()
{
return $this->dom->schemaValidate('schema.xsd');
}
public function isValid()
{
$tempFile = time() . '-' . rand() . '-document.tmp';
$this->dom->save($tempFile);
$tempDom = new DOMDocument();
$tempDom->load($tempFile);
if (is_file($tempFile))
{
unlink($tempFile);
}
return $tempDom->schemaValidate('schema.xsd');
}
?>