5
votes

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?

1

1 Answers

8
votes

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().