PHP 8.5.0 Alpha 4 available for testing

Voting

: five plus three?
(Example: nine)

The Note You're Voting On

Mike A.
19 years ago
For more detailed feedback from DOMDocument::schemaValidate, disable libxml errors and fetch error information yourself. See http://php.net/manual/en/ref.libxml.php for more info.

example.xml
<?xml version="1.0"?>
<example>
<child_string>This is an example.</child_string>
<child_integer>Error condition.</child_integer>
</example>

example.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="example">
<xs:complexType>
<xs:sequence>
<xs:element name="child_string" type="xs:string"/>
<xs:element name="child_integer" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

<?php

function libxml_display_error($error)
{
$return = "<br/>\n";
switch (
$error->level) {
case
LIBXML_ERR_WARNING:
$return .= "<b>Warning $error->code</b>: ";
break;
case
LIBXML_ERR_ERROR:
$return .= "<b>Error $error->code</b>: ";
break;
case
LIBXML_ERR_FATAL:
$return .= "<b>Fatal Error $error->code</b>: ";
break;
}
$return .= trim($error->message);
if (
$error->file) {
$return .= " in <b>$error->file</b>";
}
$return .= " on line <b>$error->line</b>\n";

return
$return;
}

function
libxml_display_errors() {
$errors = libxml_get_errors();
foreach (
$errors as $error) {
print
libxml_display_error($error);
}
libxml_clear_errors();
}

// Enable user error handling
libxml_use_internal_errors(true);

$xml = new DOMDocument();
$xml->load('example.xml');

if (!
$xml->schemaValidate('example.xsd')) {
print
'<b>DOMDocument::schemaValidate() Generated Errors!</b>';
libxml_display_errors();
}

?>

Old error message:
Warning: DOMDocument::schemaValidate() [function.schemaValidate]: Element 'child_integer': 'Error condition.' is not a valid value of the atomic type 'xs:integer'. in example.php on line 40

New error message:
DOMDocument::schemaValidate() Generated Errors!
Error 1824: Element 'child_integer': 'Error condition.' is not a valid value of the atomic type 'xs:integer'. in example.xml on line 4

<< Back to user notes page

To Top