Why Use XML Schemas?
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 becomes easier to describe the allowed content of documents.
- It becomes easier to validate the correctness of data.
- It becomes easier to work with data from databases.
- It becomes easier to define data constraints (data facets).
- It becomes easier to define data models (or data formats).
- It becomes easier to convert data between different data types.
XML Schema Uses XML Syntax
Another important feature of XML Schema is that they are written in XML.
Writing XML Schema in XML has many benefits:
- There is 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 through XML DOM.
- Schemas can be transformed through XSLT.
XML Schema Protects Data Communication
When data is sent from a sender to a receiver, the key point is that both parties should have the same "expectation" about the content.
With XML Schema, the sender can describe the data in a way that the receiver can understand.
A piece of data, such as "03-11-2004," is interpreted as November 3 in some countries and as March 11 in others.
However, an XML element with a data type, such as <date type="date">2004-03-11</date>
, ensures consistent understanding of the content because the XML data type "date" requires the format "YYYY-MM-DD."
XML Schema is Extensible
XML Schema is extensible because they are written in XML.
With extensible Schema definitions, you can:
- Reuse your Schema in other Schemas.
- Create your own data types derived from standard types.
- Reference multiple Schemas in the same document.
Well-Formedness is Not Enough
We call a document that conforms to XML syntax a well-formed XML document, such as:
- It must start with an XML declaration.
- It must have a unique root element.
- Start tags must match end tags.
- Elements are case-sensitive.
- All elements must be closed.
- All elements must be properly nested.
- Special characters must be used with entities.
Even if a document is well-formed, it still cannot guarantee that it does not contain errors, and these errors can have serious consequences.
Consider the following scenario: You ordered 5 dozen laser printers instead of 5 units. With XML Schema, most such errors would be caught by your validation software.