Validation depends on whether the schema is deterministic or non-deterministic.
A deterministic schema is a schema that is not ambiguous. This means that the parser used by the Schema Object Model (SOM) can determine the sequence in which elements should occur for an XML document to be valid.
It is possible for an XML Schema to be ambiguous, or non-deterministic. A schema is non-deterministic if the parser is unable to clearly determine the structure to validate with the schema. When validation is attempted on a non-deterministic schema, the parser generates an error.
The following deterministic XML Schema specifies that a valid document must contain an element named root
that has the following content:
apple
, followed by an element, berry
or
coffee
, followed by an element, dairy
<?xml version="1.0" encoding="utf-8" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="root" type="rootTypes" /> <xs:complexType name="myKitchen"> <xs:choice> <xs:sequence> <xs:element name="apple
"/> <xs:element name="berry
"/> </xs:sequence> <xs:sequence> <xs:element name="coffee
"/> <xs:element name="dairy
"/> </xs:sequence> </xs:choice> </xs:complexType> </xs:schema>
Using the preceding schema, the parser is able to follow the schema logic when it processes an instance document. If the element root
is followed in the document by the element apple
, the parser determines that it has encountered the first part of the sequence of apple
and berry
. Likewise, if the parser encounters the element coffee
after encountering the root
element, the parser determines that it has encountered the first part of the sequence of coffee
and dairy
. Any other ordering of elements in the instance document is invalid according to this schema.
With a non-deterministic schema, the parser cannot determine a sequence for the elements in the document being processed. The following is an example of a non-deterministic schema:
<?xml version="1.0" encoding="utf-8" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="root" type="rootTypes" /> <xs:complexType name="myKitchen"> <xs:choice> <xs:sequence> <xs:element name="apple
"/> <xs:element name="berry
"/> </xs:sequence> <xs:sequence> <xs:element name="apple
"/> <xs:element name="coffee
"/> </xs:sequence> </xs:choice> </xs:complexType> </xs:schema>
Using the preceding schema, when the parser encounters the element root
followed by an element apple
, it is unable to determine, without looking ahead to the next element, whether the apple
element is the first part of the sequence apple
and berry
or the first part of the sequence of apple
and coffee
. Because the parser used by the SOM does not perform forward checking, the parser generates the following error message when validation is attempted using a non-deterministic schema:
Content model must be deterministic