Skip to main content

XML Schema Definition (XSD)

The 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.

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.

Should I use a XML Schema?

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

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.

Well-formed and Valid XML Documents

  • An XML document is called well-formed if its syntax is correct.
  • An XML document is called valid if it is well-formed and it is validated against a XML Schema.

Example of XML document with XML Schema

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

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

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
note

Learn more in our XML Schema Tutorial.