Defines an element type for use within the Schema element.
<ElementType content="{empty | textOnly | eltOnly | mixed}" dt:type="datatype" model="{open | closed}" name="idref" order="{one | seq | many}">
empty | The element cannot contain content. |
textOnly | The element can contain only text, not elements. Note that if the model attribute is set to "open," the element can contain text and other unnamed elements. |
eltOnly | The element can contain only the specified elements. It cannot contain any free text. |
mixed | The element can contain a mix of named elements and text. Mixed is the default value. Note that if the value of the content attribute is mixed, minOccurs and maxOccurs attributes will not trigger a validation error if the number of child elements is outside the specified boundary. |
<ElementType name="x" content="empty"/> <ElementType name="x" content="textOnly"/> <ElementType name="x" content="eltOnly"> <element type="y"/> </ElementType> <ElementType name="x" content="mixed"> <element type="q"/> <element type="r"/> </ElementType>
The DTD equivalent is as follows:
empty: <!ELEMENT x EMPTY> textOnly: <!ELEMENT x (#PCDATA)> eltOnly: <!ELEMENT x y> mixed: <!ELEMENT x (#PCDATA | q | r)*>
<ElementType name="x" model="open"/>
When the model is defined as open, the element can include additional elements or attributes not declared explicitly in the content model. These additional tags can come from the same namespace or a different namespace. If they are in the same namespace, there must be a corresponding ElementType or AttributeType definition for them in the schema.
When the model is defined as closed, the element cannot include elements and cannot include mixed content not specified in the content model. The DTD uses a closed model.
<ElementType name="x"> </ElementType>
The DTD equivalent is as follows:
<!ELEMENT x EMPTY>
one | Permits only one of a set of elements. Note that for a document to correctly validate when the one attribute is specified, the model attribute for the ElementType must be specified as "closed." |
seq | Requires the elements to appear in the specified sequence. |
many | Permits the elements to appear (or not appear) in any order. If you specify "many" for the order attribute, maxOccurs values are no longer relevant during validation. |
The "seq" setting is needed to specify valid sequences. For example, it can be used to specify when a particular sequence is valid—such as "x1,y1" or "x2,y2"—but no other possible combinations are valid. The "seq" value serves the same role as parentheses in a DTD. The default value is "many."
The following example demonstrates the "one" setting. Note that the value of the model attribute must be "closed" when "one" is specified for the order attribute.
<ElementType name="z" content="eltOnly" order="one" model="closed"> <element type="x" minOccurs="1" maxOccurs="1"/> <element type="y" minOccurs="1" maxOccurs="1"/> </ElementType>
The following example demonstrates the "seq" setting:
<ElementType name="x" content="eltOnly" order="one" model="closed"> <group order="seq" content="eltOnly" model="closed"> <element type="x1"/> <element type="y1"/> </group> <group order="seq" content="eltOnly" model="closed"> <element type="x2"/> <element type="y2"/> </group> </ElementType>
The following example demonstrates the "many" setting:
<ElementType name="x" content="eltOnly" order="many" model="closed"> <element type="q"/> <element type="r"/> </ElementType>
It's important to note that the "many" setting of the order attribute makes the minOccurs and maxOccurs attributes irrelevant during validation. Rather than using the "many" setting for the order attribute in conjunction with minOccurs and maxOccurs, use the "seq" setting instead. For example, consider the following schema. Notice at the bottom of the sample that order="seq" is set.
<?xml version="1.0"?> <Schema name="SampleSchema" xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemas-microsoft-com:datatypes"> <ElementType name="PublisherID" model="closed" dt:type="ID" content="textOnly"> </ElementType> <ElementType name="PublisherName" model="closed" dt:type="string" content="textOnly"> </ElementType> <ElementType name="Publisher" model="closed" content="eltOnly" order="many"> <element type="PublisherID"/> <element type="PublisherName"/> </ElementType> <ElementType name="Book" model="closed" content="eltOnly" order="seq"> <element type="Publisher" minOccurs="1" maxOccurs="1"/> </ElementType> </Schema>
Now if the following XML document is validated against the above schema, the validation fails because the number of <Publisher> elements exceeds the specified maxOccurs value of "1". However, if the order value is set to "many," the validation would incorrectly succeed because the maxOccurs="1"
setting would be ignored.
<?xml version='1.0'?> <Book xmlns="x-schema:orderschema.xml" > <Publisher> <PublisherID>P1</PublisherID> <PublisherName>GG&G</PublisherName> </Publisher> <Publisher> <PublisherID>P2</PublisherID> <PublisherName>North</PublisherName> </Publisher> </Book>
Number of occurrences | Unlimited |
Parent elements | Schema |
Child elements | Attribute, AttributeType, datatype, description, element, group |
Requires closing tag | Yes. XML Schema is an XML grammar, and like all XML grammars, all tags must have closing tags to satisfy the definition of well-formed. |
The term "element type" refers to the element type of which all elements sharing a name are instances. Element types are declared in schemas; elements occur in documents. Element types are declared with the ElementType element type.
<ElementType name="x"> <!-- element content declarations go here --> </ElementType>