Most programming languages implement unitialized values as NULL, ZERO, random data, something special (JavaScript: undefined
) or an empty string. In XML an element can be empty in two ways and in DOM the textContent
property is an empty string for both.
Valid examples:
<TodoItem/>
<TodoItem></TodoItem>
So it should be interpreted as:
$TodoItem = "";
var TodoItem = "";
Most programming languages implement NULL values.
Best Practice:
- In the XSD: a value element may be marked as
nillable
(the default isfalse
). - In the XML: the value element marked as nillable must be marked as
nil
.
Definition example:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element type="xs:string" nillable="true"/>
</xs:schema>
Valid example:
<TodoItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
Invalid example:
<TodoItem/>
So it should be interpreted as:
$TodoItem = null;
TodoItem = null;
Most programming languages implement a mechanism to use simple types as a list. Simple types might also be called scalar types. Simple types are for example booleans, integers, strings and floats. Simple lists might be called arrays.
Best Practices:
- A list element should {must?} be a
sequence
containing one value element. - A list element should {must?} not have attributes. To not interfere with the items.
- The value elements
maxOccurs
should beunbounded
instead of the default1
. So the List can contain many items. - The value elements
minOccurs
should be0
instead of the default1
. So the List can contain zero items. - The value elements
nillable
may be set. {JavaScript array gaps? PHP numerically-indexed array preserved?}
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="TodoList">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="TodoItem" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Valid:
<TodoList>
</TodoList>
<TodoList>
<TodoItem>Beer</TodoItem>
</TodoList>
<TodoList>
<TodoItem>Eggs</TodoItem>
<TodoItem>Milk</TodoItem>
</TodoList>
Invalid:
<TodoList>
<TodoItem/>
</TodoList>
So it should be interpreted as:
$TodoList = []; // ZERO
$TodoList = ['Beer']; // ONE
$TodoList = ['Eggs', 'Milk']; // MANY
TodoList = []; // ZERO
TodoList = ["Beer"]; // ONE
TodoList = ["Eggs", "Milk"]; // MANY
Note how the element name TodoItem has no relevance to the code.
Most programming languages implement a mechanism to use non-simple types as a list too. Complex types might also be called non-scalar types, structs, classes or anonymous objects. Complex lists might be called arrays too.
Best Practices:
- *all simple list best practices apply, please read these first
- The value elements
type
may be a globally defined complex type. To improve readability only.
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="TodoItemType">
<xs:all>
<xs:element type="xs:string" name="text"/>
<xs:element type="xs:boolean" name="done" nillable="true"/>
</xs:sequence>
</xs:all>
<xs:element name="TodoList">
<xs:complexType>
<xs:sequence>
<xs:element name="TodoItem" type="TodoItemType" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Valid:
<TodoList>
</TodoList>
<TodoList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<TodoItem>
<text>beer</text>
<done xsi:nil="true">
</TodoItem>
</TodoList>
<TodoList>
<TodoItem>
<text>Eggs</text>
<done>true</done>
</TodoItem>
<TodoItem>
<text>Milk</text>
<done>false</done>
</TodoItem>
</TodoList>
Invalid example:
<TodoList>
<TodoItem>
<text/>
<TodoItem>
</TodoList>
This does not fail because the text element is empty - that is still a valid empty string. This does fail because the done element is missing, which is not allowed by the complex type.
So it should be interpreted as:
$TodoList = [];
$TodoList[0] = new stdClass();
$TodoList[0]->text = "Beer";
$TodoList[0]->done = null;
TodoList = {
"text": "Beer",
"done": null
};
Many programming languages implement a special type for key and value list. In XML Schema one might define a complex list for this.
SECTION UNDER CONSTRUCTION :: THIS DOES NOT MAKE SENSE YET
Best Practice:
- The value item should use an attribute of the simple type
string
that isrequired
.
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="TodoList">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="TodoItem" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Valid example:
<TodoList>
<TodoItem identifier="45676773">
<text>Eggs</text>
<done>true</done>
</TodoItem>
<TodoItem identifier="4d696c6b">
<text>Milk</text>
<done>false</done>
</TodoItem>
</TodoList>
So it should be interpreted as:
$TodoList = [];
$TodoList[0] = new stdClass();
$TodoList[0]->identifier = ''
$TodoList[0]->text = "Beer";
$TodoList[0]->done = null;
$TodoList[1] = new stdClass();
$TodoList[1]->identifier = ''
$TodoList[1]->text = "Beer";
$TodoList['4d696c6b']->done = null;
TodoList = {
"text": "Beer",
"done": null
};