Share via


Deterministic and Non-Deterministic Schemas

A deterministic schema is a schema that is not ambiguous, allowing the parser used by the Schema Object Model (SOM) to determine the sequence in which elements should occur in order for an XML document to be valid. It is possible for an XML Schema to be ambiguous, or non-deterministic. A schema is considered to be 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 used by the SOM generates an error.

Deterministic Schema

With a deterministic schema, the parser is able to determine what constitutes a valid sequence of elements that should occur in an instance document based on the information in the schema.

The following XML Schema example is a deterministic schema that specifies that a valid document should contain an element, root, that has content that is either an element, apple, followed by an element, berry, or an element, 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, during processing of an instance document the parser is able to follow the schema logic, which specifies that if the element root is followed in the document by the element apple, the parser 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 particular schema.

Non-Deterministic Schema

With a non-deterministic schema, the parser cannot determine a sequence for the elements in the document being processed. The following XML Schema example shows 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>

During the processing of an instance document against the preceding non-deterministic 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, if the apple element is the first part of the sequence of apple and berry or the first part of the sequence of apple and coffee. Because the parser used by the Schema Object Model (SOM) does not perform forward checking, the parser generates the error message Content model must be deterministic when validation is attempted using a non-deterministic schema.

See Also

XML Schema Object Model (SOM) | XML Schema Reference (XSD)