简体   繁体   English

当架构无效时,如何禁用DOMDocument :: schemaValidate()生成的警告?

[英]How to disable warning produced by DOMDocument::schemaValidate() when the schema is invalid?

To avoid PHP warnings in schema validation process, one can use libxml_use_internal_errors(true); 要避免模式验证过程中的PHP警告,可以使用libxml_use_internal_errors(true); , and libxml_get_errors()[0] -> message; ,和libxml_get_errors()[0] -> message; to "manually" administrate eventual validation error messages. “手动”管理最终验证错误消息。 This works when it is the XML that does not match a schema, but a warning is still being fired when the schema itself is invalid. 这在XML与模式不匹配时起作用,但在模式本身无效时仍会触发警告。

libxml_use_internal_errors(true); already captures the error message in the returned array of errors, the warning seems redundant to me, any way to bypass/disable this particular warning? 已经在返回的错误数组中捕获错误消息,警告对我来说似乎是多余的,绕过/禁用此特定警告的任何方法?

I work in strict mode, so I stop the execution when a warning fires, and log the error in a database, the problem is that the PHP warning is too vague, so I want to bypass it to report the libxml error in a separate logging system, and see the detailed error afterwards. 我在严格模式下工作,所以当警告触发时我停止执行,并在数据库中记录错误,问题是PHP警告太模糊,所以我想绕过它来报告单独记录中的libxml错误系统,然后看到详细的错误。

Is this warning a correct behavior? 这个警告是正确的行为吗? any chance it is a bug? 任何机会它都是一个bug?


The PHP code: 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
    }
?>

The PHP warning: PHP警告:

DOMDocument::schemaValidate(): Invalid Schema DOMDocument :: schemaValidate():无效的架构


The libxml detailed error message: libxml详细错误消息:

Element '{ http://www.w3.org/2001/XMLSchema }complexType': The content is not valid. 元素'{ http://www.w3.org/2001/XMLSchema } complexType':内容无效。 Expected is (annotation?, (simpleContent | complexContent | ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?)))) 预期是(注释?,(simpleContent | complexContent |((group | all | choice | sequence)?,((attribute | attributeGroup)*,anyAttribute?))))


The invalid schema (schema.xsd): 无效架构(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>

This is what I would expect to happen. 这就是我期望发生的事情。 As per the documentation, DOMDocument::schemaValidate validates a document based on a schema. 根据文档,DOMDocument :: schemaValidate基于模式验证文档。 Therefore, if the schema itself is invalid, it can't be used to validate a document. 因此,如果架构本身无效,则不能用于验证文档。

You could try and prefixing the command with @ - see http://php.net/manual/en/language.operators.errorcontrol.php . 您可以尝试使用@前缀命令 - 请参阅http://php.net/manual/en/language.operators.errorcontrol.php This should suppress the warning allowing your code to continue. 这应该取消警告,允许您的代码继续。 If that doesn't work, you could try temporarily disabling error reporting using error_reporting(0) ( http://php.net/manual/en/function.error-reporting.php ) before calling DOMDocument::schemaValidate. 如果这不起作用,您可以尝试在调用DOMDocument :: schemaValidate之前使用error_reporting(0)( http://php.net/manual/en/function.error-reporting.php )暂时禁用错误报告。 Then, restore the previous setting which would have been returned when you called error_reporting(0). 然后,恢复先前调用error_reporting(0)时返回的设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM