When trying to validate a PHP DOMDocument object against a schema by using schemaValidate method, the next warning is being generated:
Warning: DOMDocument::schemaValidate(): Element 'foo': This element is not expected. Expected is ( {http://www.example.com}foo ). in X on line Y
It only happens with elements that have been appended to the DOMDocument. I prepared the next code snippet and schema so that anybody can test instantly:
Snippet:
$template = '
<root
xmlns="http://www.example.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.com schema.xsd"
>
<bar/>
</root>
';
$DD = new DOMDocument();
$DD -> loadXML($template);
$foo = $DD -> createElement('foo');
$DD -> getElementsByTagName('root') -> item(0) -> appendChild($foo);
var_dump(htmlentities($DD -> saveXML()));
var_dump($DD -> schemaValidate(__DIR__ . '/schema.xsd'));
Schema:
<?xml version="1.0"?>
<xs:schema
targetNamespace="http://www.example.com"
xmlns:SiiDte="http://www.example.com"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified"
>
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="bar"/>
<xs:element name="foo"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I don't see the difference between foo and bar besides foo was added with appendChild method whilst bar added with loadXML method.
The validation is returning false (which means validation error). When loading foo with loadXML method, the error stop happenning, but it is definitely not the solution, because an XML need to be created dynamicaly in much cases.
¿Why appending element produces this validation error and how to solve?