Here we have a simple XML schema:
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="item">
<xs:complexType>
<xs:sequence>
<xs:element ref="item" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
<xs:attribute name="is-foo" type="xs:boolean" default="true" use="optional" />
<xs:attribute name="is-bar" type="xs:boolean" default="false" use="optional" />
</xs:complexType>
</xs:element>
</xs:schema>
Just recursive <item>
nodes with optional is-foo
/is-bar
boolean attributes, defaulting to true
and false
respectively.
Now, how are these default values useful, in the context of reading XML data in PHP? If I have a valid XML document:
<item is-foo="false" is-bar="false">
<item is-bar="true">
<item>
<item is-foo="true">
<item />
<item is-foo="true" />
</item>
</item>
<item is-foo="true" is-bar="true" />
</item>
</item>
Against which I run:
$document = new DOMDocument();
$document->load('./test.xml');
var_dump($document->schemaValidate('./test.xsd'));
$xpath = new DOMXPath($document);
foreach ($xpath->query('//item') as $item) {
echo $item->getNodePath() . PHP_EOL
. ' is-foo : ' . $item->getAttribute('is-foo') . PHP_EOL
. ' is-bar : ' . $item->getAttribute('is-bar') . PHP_EOL;
}
I get the following output (which, while expected, is unfortunate):
bool(true)
/item
is-foo : false
is-bar : false
/item/item
is-foo :
is-bar : true
/item/item/item[1]
is-foo :
is-bar :
/item/item/item[1]/item
is-foo : true
is-bar :
/item/item/item[1]/item/item[1]
is-foo :
is-bar :
/item/item/item[1]/item/item[2]
is-foo : true
is-bar :
/item/item/item[2]
is-foo : true
is-bar : true
The issue here being all of the empty attribute values.
Aside from programatically injecting default values into the documents (which would require interpreting the schema for any reusability) are there any options to transform an XML document to include default values as defined in the corresponding schema?