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 is easier to describe the allowed document content
- It is easier to verify the correctness of data
- It is easier to work with data from databases
- It is easier to define data constraints (data facets)
- It is easier to define data models (or data formats)
- It is easier to convert data between different data types
Editor's note: data constraints, or facets, are a term in the XML Schema prototype, translated into Chinese as "face", used to constrain the allowable values of a data type.
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
- You can use an XML editor to edit schema files
- You can use an XML parser to parse schema files
- Schema can be processed through XML DOM
- Schema can be transformed through XSLT
XML Schema Can Protect Data Communication
When data is sent from sender to receiver, the key point is that both parties should have the same "expectations" about the content.
With XML Schema, the sender can describe 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.
But an XML element with a data type, such as:
XML Schema Is Extensible
XML Schema is extensible because it is 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-Formed Is Not Enough
We call documents that comply with XML syntax well-formed XML documents, for example:
- It must begin with an XML declaration
- It must have a single root element
- Start tags must match end tags
- Elements are case-sensitive
- All elements must be closed
- All elements must be properly nested
- Entities must be used for special characters
Even if documents are well-formed, there is still no guarantee that they do not contain errors, and these errors may have serious consequences.
Consider the following situation: you ordered 5 dozen laser printers instead of 5. With XML Schema, most such errors will be caught by your validation software.