为什么要使用 XML Schema?

XML Schema 比 DTD 更强大。

XML Schema 支持数据类型

XML Schema 最重要的能力之一就是对数据类型的支持。

通过对数据类型的支持:

  • 可更容易地描述允许的文档内容
  • 可更容易地验证数据的正确性
  • 可更容易地与来自数据库的数据一并工作
  • 可更容易地定义数据约束(data facets)
  • 可更容易地定义数据模型(或称数据格式)
  • 可更容易地在不同的数据类型间转换数据

编者注:数据约束,或称 facets,是 XML Schema 原型中的一个术语,中文可译为“面”,用来约束数据类型的容许值。

XML Schema 使用 XML 语法

另一个关于 XML Schema 的重要特性是,它们由 XML 编写。

由 XML 编写 XML Schema 有很多好处:

  • 不必学习新的语言
  • 可使用 XML 编辑器来编辑 Schema 文件
  • 可使用 XML 解析器来解析 Schema 文件
  • 可通过 XML DOM 来处理 Schema
  • 可通过 XSLT 来转换 Schema

XML Schema 可保护数据通信

当数据从发送方被发送到接受方时,其要点是双方应有关于内容的相同的“期望值”。

通过 XML Schema,发送方可以用一种接受方能够明白的方式来描述数据。

Wani data, ma kaiya "03-11-2004", da kaiwai wani watan da 11 da 3, ma kaiya wani watan da 3 da 11.

Ama kaiya XML da kaiwai data type, ma kaiya <date type="date">2004-03-11</date>, da kaiwai yanci kaiya content da yanci kaiya, sabi a kaiya kaiya a XML data type "date" da kaiwai format da "YYYY-MM-DD".

XML Schema da kaiwai

XML Schema da kaiwai, sabi a kaiya kaiya a XML kaiwai.

Da kaiwai Schema kaiya, a kaiya kaiya:

  • Kodin kai Schema da kaiwai a Schemata
  • 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, such as:

  • 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
  • Special characters must be used with entities

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 sets of laser printers, not 5 units. Most of such errors can be captured by your validation software through XML Schema.