Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DOMDocument::schemaValidate() throwing warning errors

Tags:

php

xml

simplexml

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?

like image 357
WtotheX Avatar asked Sep 11 '12 11:09

WtotheX


People also ask

What is document XML schema verification error?

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.

What is a schema validation?

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.


1 Answers

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

like image 64
Jirka Kosek Avatar answered Sep 30 '22 14:09

Jirka Kosek