DOMDocument::schemaValidate()引发警告错误


DOMDocument::schemaValidate() throwing warning errors

我有两个文件:

  • 示例XML文件
  • 带有模式的.xsd文件,前面提到的XML文件必须遵守该文件

为了根据模式验证XML文件,我一直在使用:

$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
}

然而,每当我的XML文档不遵守模式中的规则时,我就会不断收到警告错误。

警告:DOMDocument::schemaValidate()[DOMDocument.schemaValidate]:元素"Header":不需要此元素。预期为(路由)

我一直故意搞砸我的XML文件,只是为了看看$dom->schemaValidate是如何处理它的。显然,我不希望PHP在XML不符合模式时在页面上弹出警告消息。相反,我希望我的应用程序能够解决这个问题。我是不是在俯瞰什么?

您必须调用

libxml_use_internal_errors(true);

以便抑制警告并开始将XML解析错误收集到可通过libxml_get_errors()访问的内部结构中。