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

时间:2022-10-20 18:26:59

To avoid PHP warnings in schema validation process, one can use libxml_use_internal_errors(true);, and 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.

要避免模式验证过程中的PHP警告,可以使用libxml_use_internal_errors(true);和libxml_get_errors()[0] - > message; “手动”管理最终验证错误消息。当它与模式不匹配的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?

libxml_use_internal_errors(真);已经在返回的错误数组中捕获错误消息,警告对我来说似乎是多余的,任何绕过/禁用此特定警告的方法?

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. Expected is (annotation?, (simpleContent | complexContent | ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?))))

元素'{http://www.w3.org/2001/XMLSchema}complexType':内容无效。预期是(注释?,(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>

1 个解决方案

#1


1  

This is what I would expect to happen. As per the documentation, DOMDocument::schemaValidate validates a document based on a schema. Therefore, if the schema itself is invalid, it can't be used to validate a document.

这就是我期望发生的事情。根据文档,DOMDocument :: schemaValidate基于模式验证文档。因此,如果架构本身无效,则不能用于验证文档。

You could try and prefixing the command with @ - see 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. Then, restore the previous setting which would have been returned when you called error_reporting(0).

您可以尝试使用@前缀命令 - 请参阅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)时返回的设置。

#1


1  

This is what I would expect to happen. As per the documentation, DOMDocument::schemaValidate validates a document based on a schema. Therefore, if the schema itself is invalid, it can't be used to validate a document.

这就是我期望发生的事情。根据文档,DOMDocument :: schemaValidate基于模式验证文档。因此,如果架构本身无效,则不能用于验证文档。

You could try and prefixing the command with @ - see 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. Then, restore the previous setting which would have been returned when you called error_reporting(0).

您可以尝试使用@前缀命令 - 请参阅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)时返回的设置。