Skip to main content

XML Schema Tutorial

he XML Schema is used to formally describe the grammar of XML documents, just like a DTD. In this way, a XML document can be a valid XML document.

What is a XML Schema Definition and what is it used for?

An XML Schema Definition is an XML-based alternative to DTD.

The XML Schema language is also referred to as XML Schema Definition (XSD).

It defines the elements, attributes and data types of an XML document.

It supports Namespaces.

The XML Schema aims to define the structure of XML documents in order to validate them.

Example of XML document with XML Schema

<xs:element name="book">
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="publisher" type="xs:string"/>

The Schema above is interpreted in this way:

  • <xs:element name="book"> defines the element called "note"
  • <xs:complexType> the "note" element is a complex type
  • <xs:sequence> the complex type is a sequence of elements
  • <xs:element name="title" type="xs:string"> the element to is of type string
  • <xs:element name="author" type="xs:string"> the element from is of type string
  • <xs:element name="publisher" type="xs:string"> the element heading is of type string

Should I use a XML Schema?

With XML Schema, your XML files can carry a description of its own format in order to validate them.

In the XML world, hundreds of standardized XML formats are in daily use. Many of these XML standards are defined by XML Schemas.

Should i use it? It depends on what you want to do:

  • YES, if you want to verify that the data is valid against the schema.

  • NO, if you are experimenting with XML or you have small XML files and you don't have time to create schemas.

XML Schema supports Data Types

XML Schemas support data types. With data types:

  • It is easier to describe document content
  • It is easier to define restrictions on data
  • It is easier to validate the correctness of data
  • It is easier to convert data between different data types

XML Schema uses XML Syntax

XML Schemas use XML Syntax. Thanks to XML:

  • You don't have to learn a new language
  • You can use your XML editor to edit your Schema files
  • You can use your XML parser to parse your Schema files
  • You can manipulate your Schemas with the XML DOM
  • You can transform your Schemas with XSLT