简体   繁体   中英

DOMDocument::schemaValidate() throwing warning errors

I have two files:

  • A sample XML file.
  • A .xsd file w/ schema, which the aforementioned XML file must adhere to.

To validate the XML file against the schema, I've been using:

$dom = new DOMDocument();

//$this->xmlstr; is my XML file after being loaded into a string.
$dom->loadXML($this->xmlstr); 

//$xsd_file is definitely my xsd file.
if(!$dom->schemaValidate($xsd_file)){
     $errors = libxml_get_errors(); //supposed to give back errors?
     var_dump($errors); //debugging - shows: array empty
}

However, I keep getting warning errors whenever my XML doc doesn't adhere to the rules in the schema.

Warning: DOMDocument::schemaValidate() [domdocument.schemavalidate]: Element 'Header': This element is not expected. Expected is ( Routing )

I've been intentionally screwing up my XML file, just to see how $dom->schemaValidate actually handles it. Obviously, I don't want PHP spitting out warning messages onto the page whenever the XML doesn't meet the schema. Instead, I'd like my app to take care of that. Am I overlooking something here?

You must call

libxml_use_internal_errors(true);

before creating new DOMDocument() in order to suppress warnings and start collecting XML parsing errors into internal structure accessible via libxml_get_errors().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM