XSD (XML Schema Definition) Tutorial

What is an XML Schema?

To describe the structure of an XML document, an XML Schema is used. The XML Schema Definition (XSD) is the XML Schema language.

XSD Example:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 
<xs:element name="note">
 
<xs:complextype>
  <xs:sequence>
    <xs:element name="to" type="xs:string"></xs:element>
    <xs:element name="from" type="xs:string"></xs:element>
    <xs:element name="heading" type="xs:string"></xs:element>
    <xs:element name="body" type="xs:string"></xs:element>
  </xs:sequence>
</xs:complextype>
 
</xs:element>
</xs:schema>

Explanation:

To define the legal building blocks of an XML document, the XML Schema is used. This includes the elements and attributes that can appear in a document, the number of and the order of the child elements, the data types for elements and attributes, and the default and fixed values for the elements and attributes.

Why Learn XML Schema?

Hundreds of standardized XML formats are used daily. Being an XML-based and more powerful alternative to DTD, many of these XML standards are defined by XML Schemas.

XML Schemas Support Data Types:

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

  • Easier to describe allowable document content.
  • Easier to validate the correctness of data.
  • Easier to define data facets (restrictions on data).
  • Easier to define data patterns (data formats).
  • Easier 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.

Being written in XML, XML Schemas are extensible which simply means that:

  • Our Schema can be reused in other Schemas.
  • Our own data types can be created which is derived from the standard types.
  • Multiple schemas can be referenced in the same document.

XML Schemas Secure Data Communication:

For both parts to have the same “expectations” about the content is necessary, while sending data from a sender to a receiver. The data can thus be described using XML Schemas, and by the sender, in a way that the receiver will understand.

Example:

The date “06-08-2020” could be interpreted either as 6 August or as 8 June.

An XML element, however, has a data type like this:

<date type="date">2020-08-06</date>

Now, since the XML data type “date” requires the format “YYYY-MM-DD”, it ensures a mutual understanding of the content.

Well-Formed is Not Enough:

A document that conforms to the XML syntax rules is a well-formed XML document. These rules include:

  • An XML document must begin with the XML declaration.
  • An XML document must have one unique root element.
  • The start-tags of an XML document must have matching end-tags.
  • The elements of an XML document are case sensitive.
  • All the elements of an XML document must be closed.
  • All the elements of an XML document must be properly nested.
  • All the attribute values of an XML document must be quoted.
  • The entities of an XML document must be used for special characters.

Even a well-formed XML document can still contain errors. And it certainly can have serious consequences. But to catch most of these errors by our validating software, we can take the help of the XML Schemas. 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”.

XSD Tutorial List

Please Share