To avoid PHP warnings during schema validation, you can use libxml_use_internal_errors(true);
it libxml_get_errors()[0] -> message;
to manually administer possible validation error messages. This works when it is XML that does not match the schema, but the warning is still triggered 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 for me, somehow bypassing / disabling this particular warning?
I work in strict mode, so I stop execution when a warning occurs and register an error in the database, the problem is that the PHP warning is too vague, so I want to bypass it to report the libxml error to a separate logging system and the subsequent detailed error .
Is this warning the correct behavior? Any chance this is a mistake?
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);
$DD -> schemaValidate(__DIR__ . '/schema.xsd');
$errors = libxml_get_errors();
if (isset($errors[0])) {
echo $errors[0] -> message;
}
?>
PHP warning:
DOMDocument :: schemaValidate (): invalid schema
Detailed libxml error message:
Element '{ http://www.w3.org/2001/XMLSchema } complexType': Content is not valid. Expected (annotation ?, (simpleContent | complexContent | ((group | all | choice | sequence) ?, ((attribute | attributeGroup) *, anyAttribute?))))
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"/>
</xs:complexType>
</xs:element>
</xs:schema>