简体   繁体   中英

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); , 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.

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.

Is this warning a correct behavior? any chance it is a bug?


The PHP code:

<?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:

DOMDocument::schemaValidate(): Invalid Schema


The libxml detailed error message:

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?))))


The invalid schema (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. 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 . 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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