Designing Your Own XML Schema: Indicators - The Choice indicator in XML Schema
(Page 2 of 6 )
Another indicator from the XML Schema vocabulary is the “Choice” indicator. It is specially used to inform that one of the child elements could occur. We can define as many numbers of elements as possible (with the “choice” indicator), but finally only one has to be chosen. Let us consider the following schema:
<?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="ID"
type="xs:string" />
<xs:element name="Name"
type="xs:string" />
<xs:element
name="EmployeeType">
<xs:complexType>
<xs:choice>
<xs:element name="Management" type="xs:string" />
<xs:element name="Clerk" type="xs:string" />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:key name="PK_Employee_ID">
<xs:selector xpath=".//Employee" />
<xs:field xpath="ID" />
</xs:key>
</xs:element>
</xs:schema>
The most important fragment to concentrate on is the following:
<xs:element name="EmployeeType">
<xs:complexType>
<xs:choice>
<xs:element name="Management" type="xs:string" />
<xs:element name="Clerk" type="xs:string" />
</xs:choice>
</xs:complexType>
</xs:element>
The above specifies that the “EmployeeType” element can contain “Management” or “Clerk” elements (but not both). A sample XML document, which conforms to the above schema, would be as follows:
<?xml version="1.0" encoding="UTF-8"?>
<Employees xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="C:\Documents and
Settings\Administrator\Desktop\Employees.xsd">
<Employee>
<ID>1001</ID>
<Name>Jag</Name>
<EmployeeType>
<Management>Manager</Management>
</EmployeeType>
</Employee>
<Employee>
<ID>1002</ID>
<Name>Chat</Name>
<EmployeeType>
<Clerk>Admin</Clerk>
</EmployeeType>
</Employee>
</Employees>
Next: The Sequence indicator in XML Schema >>
More XML Articles
More By Jagadish Chaterjee