XML Schema

To describe the structure of an XML document, similar to a DTD, an XML Schema is used. A DTD or Document Type Definition is also used to define the structure and the legal elements and attributes of an XML document. A “Well Formed” XML document is the one with correct syntax, while an XML document validated against an XML Schema is both “Well Formed” and “Valid”.

XML Schema:

An XML-based alternative to DTD is what an XML Schema can be simply understood as.

Example: XML Document

<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Sapna</to>
<from>Tom</from>
<heading>Meeting</heading>
<body>Meeting on Monday Morning at 9 AM.</body>
</note>

Example:

<xs:element name="note">
 
<xs:complexType>
  <xs:sequence>
    <xs:element name="to" type="xs:string"/>
    <xs:element name="from" type="xs:string"/>
    <xs:element name="heading" type="xs:string"/>
    <xs:element name="body" type="xs:string"/>
  </xs:sequence>
</xs:complexType>
 
</xs:element>

Explanation:

Interpretation of the above Schema:

  • <xs:element name=”note”>: Used to define the element called “note”.
  • <xs:complexType>: Used to define that the “note” element is a complex type.
  • <xs:sequence>: Used to define that the complex type is a sequence of elements.
  • <xs:element name=”to” type=”xs:string”>: Used to define that the element “to” is of type string (text).
  • <xs:element name=”from” type=”xs:string”>: Used to define that the element “from” is of type string.
  • <xs:element name=”heading” type=”xs:string”>: Used to define that the element “heading” is of type string.
  • <xs:element name=”body” type=”xs:string”>: Used to define that the element “body” is of type string.

Advantages of XML Schemas over DTD:

  • XML Schemas are written in XML.
  • XML Schemas are extensible to additions.
  • XML Schemas support data types.
  • XML Schemas support namespaces.

Why Use an XML Schema?

The XML files with XML Schema can carry a description of its format. Along with verifying data, independent groups of people can also agree on a standard for interchanging data with XML Schema.

XML Schemas Support Data Types:

The support for data types is one of the greatest advantages of XML Schemas because of the below reasons:

  • Easy to describe document content.
  • Easy to define restrictions on data.
  • Easy to validate the correctness of data.
  • Easy to convert data between different data types.

XML Schemas use XML Syntax:

The XML Schemas are written in XML and that is another great advantage of XML Schemas because of the below reasons:

  • No need to learn a new language.
  • The XML editor can be used to edit the Schema files.
  • The XML parser can be used to parse the Schema files.
  • The Schemas can be manipulated with the XML DOM.
  • The Schemas can be transformed with XSLT.
Please Share