Designing Your own XML Schema: Constraining with Restrictions - Restrictions based on lists (or enumerations) in XML Schema
(Page 5 of 6 )
Let us consider that we would like to have a department assigned (or specified) to every employee in our XML document. The department sometimes could be misspelled. So we need to provide a “list of values” (or enumeration) which are the only valid values to accept!
The following complete schema would do the same:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault=
"unqualified">
<xs:element name="Employees">
<xs:annotation>
<xs:documentation>Contains All Employee information</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element name="Employee" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Name" type="xs:string" />
<xs:element name="Department">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Production" />
<xs:enumeration value="Sales" />
<xs:enumeration value="Accounting" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
<xs:attribute name="ID" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
You need to observe the following fragment within the above complete schema, which actually does the restriction:
<xs:element name="Department">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Production" />
<xs:enumeration value="Sales" />
<xs:enumeration value="Accounting" />
</xs:restriction>
</xs:simpleType>
</xs:element>
You can include as many enumerations as possible within an XML schema. In the above fragment, I specified only “string” based enumerations. You can also work with “integer,” “date” and other types of enumerations as well.
Next: Defining a Primary Key in XML Schema >>
More XML Articles
More By Jagadish Chaterjee