The purpose of an XML schema is to define the building blocks of an XML document. An XML schema defines the following in an XML document:
An XML schema enables you to combine related elements and attributes into groups. This Feature of creating grouped elements and attributes enables you to perform the following tasks:
Consider the following example:
<xsd:schema xmlns:xsd=”http://www.w3.org/2001/XMLSchema”>
<xsd:group name=”empname”>
<xsd:sequence>
<xsd:element name=”FIRSTNAME” type=”xsd:string”/>
<xsd:element name=”LASTNAME” type=”xsd:string/>
</xsd:sequence>
</xsd:group>
<xsd:element name=”EMPLOYEE” type=”emptype”/>
<xsd:ComplexType name=”emptype”>
<xsd:sequence>
<xsd:group ref=”empname”/>
<xsd:element name=”ADDRESS” type=”xsd:string”/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
In the preceding example, the FIRSTNAME and LASTNAME elements are grouped together by a common name called empname by using the group element. The order in which these elements must appear in the XML document is specified using the sequence element. You can refer to the empname group while creating a complex type by using the following Statement:
<xsd:group ref=”empname”/>
After declaring the elements are attributes in the preceding schema, you can create an XML document that conforms to the schema by using the following code snippet:
<?xml version=”1.0?>
<EMPLOYEE>
<FIRSTNAME> Sam </FIRSTNAME>
<LASTNAME> Peterson </LASTNAME>
<ADDRESS> 10, LIONS STREET, BOSTON </ADDRESS>
</EMPLOYEE>
The choice Element
XSD enables you to select a a single option from multiple options by using the choice element. The choice element allows only one of the elements contained in the group to be present within the parent element.
The syntax for declaring a choice element is as follows:
<choice id=”ID” maxOccurs=”nonNegativeInteger | unbounded”
minOccurs=”nonNegativeInteger”> </choice>
In the preceding syntax, id, maxOccurs, and minOccurs are attributes of the choice element. These attributes are similar to the attributes of the group element mentioned earlier.
Consider the following example. You may want to store either the office address or the residential address of a customer in an XML document. You can implement this option by using the choice element, as shown in the following code:
<xsd:schema xmlns:xsd=”http://www.w3.org/2001/XMLSchema”>
<xsd:element name=”CUSTOMER” type=”custtype”/>
<xsd:complexType name=”custtype”>
<xsd:sequence>
<xsd:group ref=”custname”/>
<xsd:element name=”ADDRESS” type=”addtype”/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name=”addtype”>
<xsd:choice>
<xsd:element name=”RESIDENCE” type=”xsd:string”/>
<xsd:element name=”OFFICE” type=”xsd:string”/>
</xsd:choice>
</xsd:complexType>
<xsd:group name=”custname”>
<xsd:sequence>
<xsd:element name=”FIRSTNAME” type=”xsd:string”/>
<xsd:element name=”LASTNAME” type=”xsd:string/>
</xsd:sequence>
</xsd:group>
</xsd:schema>
In the preceding code, the CUSTOMER element is declared with a reference to the complex
Type, custtype. This type further refers to the custname group, which contains the Declarations for the FIRSTNAME and LASTNAME elements. The custtype complex type also contains the declaration for the ADDRESS element, which refers to the complex type, addtype. This complex type contains the declerations for the RESIDENCE and OFFICE elements. These declerations appear within the choice element. Therefore, only one of these elements can be used at a time within the ADDRESS element.
The following XML doument conforms to the preceding schema:
<?xml version=”1.0”?>
<CUSTOMER>
<FIRSTNAME> Sam </FIRSTNAME>
<LASTNAME> Peterson </LASTNAME>
<ADDRESS>
<RESIDENCE>10, LIONS STREET, BOSTON </RESIDENCE>
</ADDRESS>
</CUSTOMER>
If you include the OFFICE element within the preceding code, it will result in an error During document validation.
The all Element
In contrast to the sequence element, the all element enables you to use the child elements in any order.
The syntax for using the all element is as follows:
<all maxOccurs=”posiveInteger” minOccurs=”0|1”> </all>
In the preceding syntax, the maxOccurs and minOccurs attributes have the same meaning as the maxOccurs and minOccurs element elements of the group element.
The following code contains the declaration for the EMPLOYEE element and its child elements. The code is the same as the one given for the group element. However, the sequence element has been replaced by the all element here.
<xsd:schema xmlns:xsd=”http://www.w3.org/2001/XMLSchema”>
<xsd:element name=”EMPLOYEE” type=”emptype” />
<xsd:complexType name=”emptype”>
<xsd:all>
<xsd:element name=”FIRSTNAME” type=”xsd:string”/>
<xsd:element name=”LASTNAME” type=”xsd:string”/>
<xsd:element name=”DESIG” type=”xsd:string/>
<xsd:element name=”DEPARTMENT” type=”xsd:string/>
</xsd:all>
</xsd:complexType>
</xsd:schema>
The attributeGroup Element
XSD enables you to group attributes that can be reused with different elements. You can group attributes by using the attributeGroup element:
The syntax of the attributeGroup element is as follows:
<attributeGroup>
attribute1
attribute2
:
</attributeGroup>
In the syntax, the attribute1 and attribute2 statements declare the various attributes that are to be grouped. The following example illustrates the use of the attributeGroup element. This code is used to declare the EMPLOYEE element, which contains the FIRSTNAME and LASTNAME elements. The EMPLOYEE element also contains the DEPARTMENT and DESIGNATION attributes.
<xsd:schema xmlns:xsd=”http://www.w3.org/2001/XMLSchema”>
<xsd:element name=”EMPLOYEE” type=”emptype”/>
<xsd:complexType name=”emptype”>
<xsd:group ref=”empname”/>
<xsd:attributeGroup ref=”depdesig/>
</xsd:ComplexType>
<xsd:group name=”empname”>
<xsd:sequence>
<xsd:element name=”FIRSTNAME” type=”xsd:string”/>
<xsd:element name=”LASTNAME” type=”xsd:string”/>
</xsd:sequence>
</xsd:group>
<xsd:attributeGroup name=”depdesig”>
<xsd:attribute name=”DEPARTMENT” type=”xsd:string”/>
<xsd:attribute name=”DESIGNATION” type=”xsd:string”/>
</xsd:attributeGroup>
</xsd:schema>
In the preceding example, the emptype complex type has a reference to the depdesig attribute group, which contains the declarations for the DEPARTMENT and DESIGNATION attributes.
You can use the elements and attributes declared in the preceding schema as follows:
<?xml version=”1.0”?>
<EMPLOYEE DEPARTMENT=”Mktg” DESIGNATION=”Mgr”>
<FIRSTNAME> James </FIRSTNAME>
<LASTNAME> Wallace </LASTNAME>
</EMPLOYEE>
An attributeGroup element is typically used in cases where the same set of attributes is Used in multiple elements within the schema.