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>