I have built a \DomDocument in PHP which I'd like to validate against an XSD file. I've checked in more online XML-XSD validator and my XML passed validation on all of them. What am I doing wrong? Why my XML passes other validators but not when calling schemaValidate on the DomDocument itself?
This is the piece of PHP code that generates the XML:
$xmlDoc = new \DOMDocument('1.0', 'UTF-8');
$rootElem = $xmlDoc->createElementNS('http://fip.loginet.hu', 'allatok');
/** @var RearingAnimal $animal */
foreach($this->animals as $animal){
$animalElem = $xmlDoc->createElement('allat');
$animalElem->appendChild($xmlDoc->createElement('fulszam', $animal->getEarNumber()));
$animalElem->appendChild($xmlDoc->createElement('tenyeszet', $animal->getRearingCode()));
$animalElem->appendChild($xmlDoc->createElement('szuletesi_ido', $animal->getBirthDate()));
$animalElem->appendChild($xmlDoc->createElement('fajta', $animal->getBreed()));
$animalElem->appendChild($xmlDoc->createElement('ivar', $animal->getSex()));
$rootElem->appendChild($animalElem);
}
$xmlDoc->appendChild($rootElem);
if(!$xmlDoc->schemaValidate($this->getXsdFileName())){
throw new \Exception("XML Socument validation failed!");
}
XSD:
<schema
attributeFormDefault="unqualified"
elementFormDefault="qualified"
targetNamespace="http://fip.loginet.hu"
xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://fip.loginet.hu">
<element name="allatok">
<complexType>
<sequence>
<element name="allat" minOccurs="0" maxOccurs="unbounded" type="tns:Allat"/>
</sequence>
</complexType>
</element>
<complexType name="Allat">
<sequence>
<element name="fulszam" type="string"/>
<element name="tenyeszet" type="integer"/>
<element name="szuletesi_ido" type="date"/>
<element name="fajta" type="integer"/>
<element name="ivar" type="tns:Ivar"/>
</sequence>
</complexType>
<simpleType name="Ivar">
<restriction base="string">
<enumeration value="m"/>
<enumeration value="f"/>
</restriction>
</simpleType>
And the XML i'd like to validate against:
<?xml version="1.0" encoding="UTF-8"?>
<allatok xmlns="http://fip.loginet.hu">
<allat>
<fulszam>HU 30966 0259 0</fulszam>
<tenyeszet>4737016</tenyeszet>
<szuletesi_ido>2016-09-03</szuletesi_ido>
<fajta>1</fajta>
<ivar>m</ivar>
</allat>
<allat>
<fulszam>HU 31342 0375 1</fulszam>
<tenyeszet>4737016</tenyeszet>
<szuletesi_ido>2016-03-21</szuletesi_ido>
<fajta>2</fajta>
<ivar>m</ivar>
</allat>
<allat>
<fulszam>HU 31342 4595 1</fulszam>
<tenyeszet>4737016</tenyeszet>
<szuletesi_ido>2016-03-21</szuletesi_ido>
<fajta>2</fajta>
<ivar>m</ivar>
</allat>
</allatok>
The error I get:
Warning: DOMDocument::schemaValidate(): Element 'allat': This element is not expected. Expected is ( {http://fip.loginet.hu}allat )
UPDATE:
I figured out, that the warning can be fixed if I use $xmlDoc->createElementNS
and pass the namespace individually everywhere, instead of using $xmlDoc->createElement
. However the output of the both is the same XML string. However the xmlns
definition of the 'allatok'
element should apply to all descendant elements until stated else... so I solved it, but I'm still curious if someon could explain this behavior?