XML Menu


XML Schema




The short name for XML Schema is XSD(XML schema definition). An XML schema is a document or a set of documents that defines the structure and legal elements of the XML file. XML schemas can be used to ensure that an XML file is syntactically correct and conforms to the defined schema.

XML Schema have a number of advantages over DTDs.

  • Schema use XML syntax, so the same tools for processing XML data (e.g., parsers, DOM, SAX, etc.) can be used for processing Schema.
  • Schema support a richer set of data types, allows users to derive their own data types.
  • Schema allow more flexible and powerful definitions of content models.
  • Schema are extensible, supporting easier reuse of parts of schemas in other schemas (e.g., easier use of Namespaces).
  • Schema have annotation elements that provide greater documentation of structural design.

<schema> Element

The <schema> element is the root element of every XML Schema. The <schema> element may contain some attributes. A schema declaration often looks something like this:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
...
...
</xs:schema>

indicates that the elements and data types used in the schema come from the "http://www.w3.org/2001/XMLSchema" namespace.


Referencing a Schema in an XML file

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="file_name.xsd">
  <child>
    <subchild>.....</subchild>
  </child>
</root> 

Elements declarations

here are two types of Elements declarations in XML schema.
  • simpleType
  • complexType

simpleType

A simple element is an XML element that can contain only text. It cannot contain any other elements or attributes.

Syntax
<xs:element name="element_name" type="data_type" minOccurs="value" maxOccurs="value" />  

NOTE: minOccurs and maxOccurs specify how many occurrences of the element may appear in an XML document.The default value for minOccurs is 1 and unbounded is used to specify an upper limits in maxOccurs.

example
<xs:element name="name" type="xs:string"/>
<xs:element name="rollno" type="xs:integer"/> 

complexType

The complexType allows you to hold multiple elements. It can contain additional sub elements or can be left empty.

Syntax
<xs:element name="parent_element_name">
<xs:complexType>
    <xs:sequence>
      <xs:element name="child_element_name" type="data_type"/>
      <xs:element name="child_element_name" type="data_type"/>
    </xs:sequence>
  </xs:complexType>
 </xs:element>
  

example
<xs:element name="student" maxOccurs="unbounded">
<xs:complexType>
    <xs:sequence>
		<xs:element name="name" type="xs:string"/>
		<xs:element name="rollno" type="xs:integer"/> 
    </xs:sequence>
  </xs:complexType>
 </xs:element>

Datatypes in XSD

XML Schema has a lot of built-in data types. The most common types are:

  • xs:string
  • xs:decimal
  • xs:integer
  • xs:boolean
  • xs:date
  • xs:time

Attributes declarations

All attributes are declared as simple types. If an element has attributes, element is considered to be of a complex type.

Syntax
<xs:attribute name="attribute_name" type="data_type" />  

Default Values for Attributes

Attributes may have a default value. A default value is automatically assigned to the attribute when no other value is specified.

Syntax
<xs:attribute name="attribute_name" type="data_type" default="value"/>  

Fixed Values for Attributes

Attributes may have a fixed value specified. A fixed value is also automatically assigned to the attribute, and you cannot specify another value.

Syntax
<xs:attribute name="attribute_name" type="data_type" fixed="value"/>  

Optional and Required Attributes

Attributes are optional by default. To specify that the attribute is required, use the "use" attribute:

Syntax
<xs:attribute name="attribute_name" type="data_type" use="required"/>  


XML Schema Example-1

department.xml
<department xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="student.xsd">
  <student branch="CSE">
    <name>Ramu</name>
	<rollno>501</rollno>
  </student>
  <student branch="ECE">
    <name>Arun</name>
	<rollno>401</rollno>
  </student>
</department> 
student.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="department">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="student" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="name" type="xs:string" />
              <xs:element name="rollno" type="xs:integer" />
            </xs:sequence>
            <xs:attribute name="branch" type="xs:string" use="required" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Next Topic :DOM Parser