Problem brzmi: PHP nie chce mi interpretować domyślnych przestrzeni nazw w czasie parsowania XMLa przy użyciu XML Schema.
W wersji okrojonej, która jednak nadal powoduje takie same objawy:
plik XML Schema:
<?xml version="1.0" encoding="utf-8"?> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:myNS" xmlns="urn:myNS" xmlns:wf="urn:myNS"> <!-- protocol version --> <xsd:attribute name="version" type="xsd:string" fixed="0.0.1"> <xsd:annotation> <xsd:documentation xml:lang="en">Current version of protocol. At the moment only "0.0.1" is supported.</xsd:documentation> </xsd:annotation> </xsd:attribute> <!-- backed XML format --> <xsd:complexType name="transport"> <xsd:annotation> <xsd:documentation xml:lang="en">Structure of backend XML protocol.</xsd:documentation> </xsd:annotation> <xsd:attribute ref="wf:version" use="required" /> </xsd:complexType> <!-- root node --> <xsd:element name="transport" type="wf:transport"> <xsd:annotation> <xsd:documentation xml:lang="en">Root element for XML backend protocol.</xsd:documentation> </xsd:annotation> </xsd:element> </xsd:schema>
plik docelowy:
<?xml version="1.0" encoding="utf-8"?> <transport xmlns="urn:myNS" version="0.0.1" />
walidowanie:
<?php $xml = new DOMDocument(); $xml->load('plik.xml'); $xml->schemaValidate('schema.xsd'); ?>
efekt:
Cytat
Warning: DOMDocument::schemaValidate(): Element '{urn:myNS}transport', attribute 'version': The attribute 'version' is not allowed. in /home/wrzasq/public_html/test.php on line 34
Warning: DOMDocument::schemaValidate(): Element '{urn:myNS}transport': The attribute '{urn:myNS}version' is required but missing. in /home/wrzasq/public_html/test.php on line 34
Warning: DOMDocument::schemaValidate(): Element '{urn:myNS}transport': The attribute '{urn:myNS}version' is required but missing. in /home/wrzasq/public_html/test.php on line 34
Jak widać problem jest z przestrzenią nazw. Mimo zadeklarowania domyślnej, PHP nadal nie traktuje atrybutu jako należącego do danej przestrzeni. Co ciekawe ten problem nie dotyczy elementów (tak jak mówiłem to wersja okrojona do lepszego zobrazowania problemu - w wersji pełnej typy danych są dużo bardziej złożone, ale jednak elementy są traktowane poprawnie jako należące do domyślnej przestrzeni nazw).