DOMDocument->schemaValidate()

(no version information, might be only in CVS)

DOMDocument->schemaValidate() --  Validates a document based on a schema

说明

class DOMDocument {

bool schemaValidate ( string filename )

}

Validates a document based on the given schema file.

参数

filename

The path to the schema.

返回值

如果成功则返回 TRUE,失败则返回 FALSE


add a note add a note User Contributed Notes
Mike A.
18-Feb-2006 04:03
For more detailed feedback from DOMDocument::schemaValidate, disable libxml errors and fetch error information yourself.  See http://php.net/manual/en/ref.libxml.php for more info.

example.xml
<?xml version="1.0"?>
<example>
   <child_string>This is an example.</child_string>
   <child_integer>Error condition.</child_integer>
</example>

example.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
   <xs:element name="example">
       <xs:complexType>
           <xs:sequence>
               <xs:element name="child_string" type="xs:string"/>
               <xs:element name="child_integer" type="xs:integer"/>
           </xs:sequence>
       </xs:complexType>
   </xs:element>
</xs:schema>

<?php

function libxml_display_error($error)
{
  
$return = "<br/>\n";
   switch (
$error->level) {
       case
LIBXML_ERR_WARNING:
          
$return .= "<b>Warning $error->code</b>: ";
           break;
       case
LIBXML_ERR_ERROR:
          
$return .= "<b>Error $error->code</b>: ";
           break;
       case
LIBXML_ERR_FATAL:
          
$return .= "<b>Fatal Error $error->code</b>: ";
           break;
   }
  
$return .= trim($error->message);
   if (
$error->file) {
      
$return .=    " in <b>$error->file</b>";
   }
  
$return .= " on line <b>$error->line</b>\n";

   return
$return;
}

function
libxml_display_errors() {
  
$errors = libxml_get_errors();
   foreach (
$errors as $error) {
       print
libxml_display_error($error);
   }
  
libxml_clear_errors();
}

// Enable user error handling
libxml_use_internal_errors(true);

$xml = new DOMDocument();
$xml->load('example.xml');

if (!
$xml->schemaValidate('example.xsd')) {
   print
'<b>DOMDocument::schemaValidate() Generated Errors!</b>';
  
libxml_display_errors();
}

?>

Old error message:
Warning: DOMDocument::schemaValidate() [function.schemaValidate]: Element 'child_integer': 'Error condition.' is not a valid value of the atomic type 'xs:integer'. in example.php on line 40

New error message:
DOMDocument::schemaValidate() Generated Errors!
Error 1824: Element 'child_integer': 'Error condition.' is not a valid value of the atomic type 'xs:integer'. in example.xml on line 4
jasonoleary -at- hotmail
07-Dec-2005 01:12
regarding Warning messages for invalid XML documents, you can adjust the previous post to the following for simplicity

$validity =@ $dom->schemaValidate("mySchema.xsd") ;
if ( $validity === true )
   print "valid schema";
else
   print "NOT valid";
c_daut_d at earthlink dot net
14-Dec-2004 08:18
In 5.0.2 for Mac OS X, schemaValidate DOES generate a warning (various differing depending on what's wrong) if the XML doesn't validate.

Which means that simply checking the return value will not protect you from poorly formed XML files. You still have to implement error checking (try catch blocks won't help because it's an error not an exception).

Moreover, what I don't understand is why does it (and other DOM functions such as DOMDocument->load) generate an error instead of throwing a DOMException.
gem at rellim dot com
05-Nov-2004 10:31
Generates PHP WARNINGs if the schema is invalid.
Does not generate WARNINGs if the xml is invalid.

Here is a simple usage example:

<?php

$xml
= new DOMDocument();
$xml->load('my.xml');
if (
$xml->schemaValidate("my.xsd")) {
     echo
"Validated OK";
} else {
     echo
"Validate FAILED";
}

?>