[英]How to disable warning produced by DOMDocument::schemaValidate() when the schema is invalid?
要避免模式驗證過程中的PHP警告,可以使用libxml_use_internal_errors(true);
,和libxml_get_errors()[0] -> message;
“手動”管理最終驗證錯誤消息。 這在XML與模式不匹配時起作用,但在模式本身無效時仍會觸發警告。
libxml_use_internal_errors(true);
已經在返回的錯誤數組中捕獲錯誤消息,警告對我來說似乎是多余的,繞過/禁用此特定警告的任何方法?
我在嚴格模式下工作,所以當警告觸發時我停止執行,並在數據庫中記錄錯誤,問題是PHP警告太模糊,所以我想繞過它來報告單獨記錄中的libxml錯誤系統,然后看到詳細的錯誤。
這個警告是正確的行為嗎? 任何機會它都是一個bug?
PHP代碼:
<?php
$DD = new DOMDocument('1.0', 'ISO-8859-1');
$DD -> loadXML('<?xml version ="1.0" encoding="ISO-8859-1"?><a></a>');
libxml_use_internal_errors(true); // NO LIBXML WARNINGS
$DD -> schemaValidate(__DIR__ . '/schema.xsd'); // Vague WARNING
$errors = libxml_get_errors();
if (isset($errors[0])) {
echo $errors[0] -> message; // Libxml detailed message
}
?>
PHP警告:
DOMDocument :: schemaValidate():無效的架構
libxml詳細錯誤消息:
元素'{ http://www.w3.org/2001/XMLSchema } complexType':內容無效。 預期是(注釋?,(simpleContent | complexContent |((group | all | choice | sequence)?,((attribute | attributeGroup)*,anyAttribute?))))
無效架構(schema.xsd):
<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema
targetNamespace="http://www.lala.com/la"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:la="http://www.lala.com/la"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
>
<xs:element name="foo">
<xs:complexType>
<xs:element ref="bar"/><!-- lacking <sequence> parent -->
</xs:complexType>
</xs:element>
</xs:schema>
這就是我期望發生的事情。 根據文檔,DOMDocument :: schemaValidate基於模式驗證文檔。 因此,如果架構本身無效,則不能用於驗證文檔。
您可以嘗試使用@前綴命令 - 請參閱http://php.net/manual/en/language.operators.errorcontrol.php 。 這應該取消警告,允許您的代碼繼續。 如果這不起作用,您可以嘗試在調用DOMDocument :: schemaValidate之前使用error_reporting(0)( http://php.net/manual/en/function.error-reporting.php )暫時禁用錯誤報告。 然后,恢復先前調用error_reporting(0)時返回的設置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.