note 71014 added to function.dom-domdocument-schemavalidate
| From: | justin at redwiredesign dot com | Date: | Wed, 08 Nov 2006 11:32:48 +0000 |
| Subject: | note 71014 added to function.dom-domdocument-schemavalidate | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-119508@lists.php.net to get a copy of this message | ||
In his previous comment, Mike A wrote about validating documents using an XSD. However, you can
validate without one. In my case, I needed to ensure that the content entered was just valid XML or
not, and I couldn't find an XSD to support that. So I wrote this:
public static function validate($xml)
{
libxml_use_internal_errors(true);
$doc = new DOMDocument('1.0', 'utf-8');
$doc->loadXML($xml);
$errors = libxml_get_errors();
if (empty($errors))
{
return true;
}
$error = $errors[0];
if ($error->level < 3)
{
return true;
}
$lines = explode("\r", $xml);
$line = $lines[($error->line)-1];
$message = $error->message.' at line '.$error->line.':<br
/>'.htmlentities($line);
return $message;
}
The catch here is that the function only checks for the first error is LIBXML_ERR_FATAL, which would
break XSL/XML compilation.
In my experience, the errors are returned by libxml_get_errors in descending severity, so this may
be an OK thing to do.
----
Server IP: 66.163.161.117
Probable Submitter: 87.194.35.152
----
Manual Page -- http://www.php.net/manual/en/function.dom-domdocument-schemavalidate.php
Edit -- https://master.php.net/note/edit/71014
Del: integrated -- https://master.php.net/note/delete/71014/integrated
Del: useless -- https://master.php.net/note/delete/71014/useless
Del: bad code -- https://master.php.net/note/delete/71014/bad+code
Del: spam -- https://master.php.net/note/delete/71014/spam
Del: non-english -- https://master.php.net/note/delete/71014/non-english
Del: in docs -- https://master.php.net/note/delete/71014/in+docs
Del: other reasons-- https://master.php.net/note/delete/71014
Reject -- https://master.php.net/note/reject/71014
Search -- https://master.php.net/manage/user-notes.php