I have two files:
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?
Schema errors occur where there is a problem with the structure or order of the file, or an invalid character is included. Schema errors prevent the validation being run in full because the file cannot be read. This means that errors cannot be traced to a particular record.
An API schema defines which API requests are valid based on several request properties like target endpoint and HTTP method. Schema Validation allows you to check if incoming traffic complies with a previously supplied API schema.
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().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With