XML schema is a language for describing the contents and structure of a XML document. It basically creates a blue print of the actual XML document by describing everything about the elements which can appear in the document. By creating a XML schema we make the XML document conform to the rules defined in the XML schema. It essentially says that the XML document cant break the rules defined in the XML schema document.
also read:
Lets create a sample XML schema document empSchema.xsd:
<?xml version="1.0" encoding="ISO-8859-1" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="employee"> <xs:complexType> <xs:sequence> <xs:element name="id" type="xs:string"/> <xs:element name="empName" type="xs:string"/> <xs:element name="salary" type="xs:string"/> <xs:element name="domain" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
Lets create a XML document employee.xml:
<?xml version="1.0"?> <employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="empSchema.xsd"> <id>745821</id> <empName>Tim</empName> <salary>35000</salary> <domain>Insurance</domain> </employee>
In the XML document above we see that the document is referring to a XSD document[xsi:noNamespaceSchemaLocation=”empSchema.xsd”]. XSD is nothing but a file which defines XML schema.
I will explain the details of the XML schema document as we go along. The above example is just supposed to give you a feel of what an XML schema looks like and how to refer to the XSD from a XML file.
XML Schema Elements
schema
Every XML schema starts with element as the root. The attribute xmlns:xs=”http://www.w3.org/2001/XMLSchema” says that the elements in the XML document belong to the “http://www.w3.org/2001/XMLSchema” namespace and each of the elements that belong to this namespace should be prefixed with the xs.
Simple Type
A simple type element cannot contain any other elements or attributes. It can contain only text data. For E.g:
<table> <tr> <td>Syntax:</td> <td><xs:element name="element-name" type="data-type"/></td> </tr> <tr> <td>XML Schema:</td> <td><xs:element name="empName" type="xs:string"/></td> </tr> <tr> <td>XML:</td> <td><empName>Daniel</empName></td> </tr> </table>
The allowed datatypes in XML schema are :
xs:string |
xs:decimal |
xs:integer |
xs:boolean |
xs:date |
xs:time |
Attributes
An attribute provides additional information about the element. Simple types cannot have attributes. However the attribute itself are of type simple. Let us now look at the syntax and an example of how to define attributes in XML Schema:
<table> <tr> <td>Syntax:</td> <td><xs:attribute name="attribute-name" type="data-type"/></td> </tr> <tr> <td>XML Schema:</td> <td><xs:attribute name="grade" type="xs:string"/></td> </tr> <tr> <td>XML:</td> <td><employee grade="senior">Daniel</employee></td> </tr> </table>
Content Restrictions
When an element or an attribute is defined with a particular data type, it essentially means that the element or attribute cant be assigned a value which is of some other type. In XML schema we can define custom restrictions on elements. Such restrictions are called facets.
- Value restrictions
Using value restrictions the XML schema places a restriction that the value of the element should be between a allowed range.In the following example element a restriction is placed on the salary element that its value cannot be less that zero and more than 85000.
In case it happens then the XML document will not validate:
<xs:element name="salary"> <xs:simpleType> <xs:restriction base="xs:integer"> <xs:minInclusive value="0"/> <xs:maxInclusive value="85000"/> </xs:restriction> </xs:simpleType> </xs:element>
In set restriction the value of an element should be set from only an allowed set of values. In the following example the value of the domain element is limited to a certain set of values:
<xs:element name="domain"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="BFS"/> <xs:enumeration value="Insurance"/> <xs:enumeration value="Manlog"/> <xs:enumeration value="Life Sciences"/> </xs:restriction> </xs:simpleType> </xs:element>
In such type of restriction we can use any valid regular expression syntax to define the restrictions. In the following example we define a restriction that the number should be between 0 and 9.
<xs:element name="number"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[0-9]"/> </xs:restriction> </xs:simpleType> </xs:element>
These restrictions define how the whitespace characters are handled in the XML document. The restriction can be placed using the following syntax:
<xs:whiteSpace value="preserve"/> or <xs:whiteSpace value="replace"/>
The attribute value preserve says that the whitespace characters will not be removed by the XML processor. The attribute value replace says that the following characters will be replaced by spaces :
line feeds, tabs, spaces, and carriage returns. Lets looks at an example now :
<xs:element name="feedback"/> <xs:simpleType> <xs:restriction base="xs:string"/> <xs:whiteSpace value="preserve"/> </xs:restriction/> </xs:simpleType/> </xs:element/>
This places a restriction on the allowed numbers of characters. The following example places a restriction that the employee id cannot be more than 6 characters
<xs:element name="empId"> <xs:simpleType> <xs:restriction base="xs:integer"> <xs:length value="6"/> </xs:restriction> </xs:simpleType> </xs:element>