Why use XML Schema?

XML Schema is more powerful than DTD

XML Schema supports data types

One of the most important capabilities of XML Schema is its support for data types.

Through support for data types:

  • It can be easier to describe the allowed document content
  • It can be easier to verify the correctness of data
  • It can be easier to work with data from databases
  • Data constraints (data facets) can be defined more easily
  • Data models (or data formats) can be defined more easily
  • Data can be converted more easily between different data types

Editor's note:Data constraints, or facets, are a term in the XML Schema prototype, which can be translated into Chinese as "face", used to constrain the permissible values of data types.

XML Schema uses XML syntax

Another important feature of XML Schema is that they are written in XML.

There are many benefits to writing XML Schema in XML:

  • No need to learn a new language
  • XML editors can be used to edit Schema files
  • XML parsers can be used to parse Schema files
  • Schemas can be processed using XML DOM
  • Schemas can be transformed using XSLT

XML Schema can protect data communication

When data is sent from the sender to the recipient, the key point is that both parties should have the same "expectations" about the content.

Through XML Schema, the sender can describe the data in a way that the recipient can understand.

A date, such as "03-11-2004", is interpreted as November 3rd in some countries, and as March 11th in others.

However, an XML element with a data type, such as: <date type="date">2004-03-11</date>, can ensure a consistent understanding of the content, because the XML data type "date" requires the format of "YYYY-MM-DD".

XML Schema is extensible

XML Schema is extensible because they are written in XML.

Through extensible Schema definitions, you can:

  • Reuse your Schema in other Schemas
  • Create your own data types derived from standard types
  • Refer to multiple Schemas in the same document

Well-formed is not enough

We call documents that conform to XML syntax well-formed XML documents, for example:

  • It must start with an XML declaration
  • It must have a unique root element
  • The start tag must match the end tag
  • Elements are case-sensitive
  • All elements must be closed
  • All elements must be correctly nested
  • Entities must be used for special characters

Even if the document is well-formed, it cannot be guaranteed that it will not contain errors, and these errors may have serious consequences.

Consider the following situation: You ordered 5 cartons of laser printers instead of 5 units. Through XML Schema, most of such errors can be captured by your validation software.