跳转到内容

XML Schema

本页使用了标题或全文手工转换
维基百科,自由的百科全书

这是本页的一个历史版本,由CamLine留言 | 贡献2017年8月26日 (六) 12:34 Secondary uses for XML Schemas编辑。这可能和当前版本存在着巨大的差异。

XSD (XML Schema Definition)'W3C于2001年5月发布的推荐标准,指出如何形式描述XML文档的元素。XSD是許多XML Schema 语言中的一支。XSD是首先分離於XML本身的schema語言,故取得W3C的推薦地位。

像所有XML Schema 语言一樣,XSD用來描述一組规则──一个XML文件必須遵守这些規则,才能根據該schema『合法(Valid)』。

然而,与其他XML Schema 语言不同,XSD意圖設計为在确认一个文档的有效性时,将会产生满足特定数据类型的一个信息集合。这种后验证的XML信息集可用来開發XML文件處理軟件。

XSD名称的来源

因為有其他XML schema 語言存在,故在引用這W3C建議的語言時,使用XML Schema或W3C XML Schema,Schema永遠字首大寫。

“XML Schema”在2001年5月成为W3C推荐标准。由于“XML Schema”作为一种W3C的推荐标准的名字与广义的XML Schema 语言存在名称上的混淆,用户社区的一部分人采用了“WXS”来称呼它, 用户社区的另一部分人采用“XSD”(XML Schema Definition首字母縮略字)来称呼它。W3C发布的1.1标准采用了“XSD”作为官方称呼。

歷史

在官方文档的參考附錄里,XSD标准承認受到[文件类型描述|DTD]]和其他早期XML schema 语言的影响,如DDML英语DDMLSOX英语Schema for Object-Oriented XMLXML-Data英语XML-Data、以及XDR英语XML data reduced。XSD從中吸收了一些特性,然而也在這些特性中有所折衷。這些早期schema 語言中的XDR與SOX在XML Schema發布後仍繼續使用了一段时间。不少微軟的產品支援XDR直到2006年十二月MSXML 6.0的發佈(MSXML 6.0拋棄了XDR改用XSD)[1]Commerce One, Inc支援它自己的SOX schema 語言直到該公司於2004年末破產。2004年十二月,Novell, Inc.購買了該公司,包括那些與SOX相關的專利,據報導是盡力防止被某些不相關的、以打專利相關官司為生的公司剝削圖利[2]

著名的XSD建议的内容但在XML自己的DTD中不可用的特性是命名空间感知(namespace awareness)与数据类型。

2012年4月, XSD 1.1成为W3C推荐标准。April 2012

Schema与schema文档

技术上说schema是元数据的一个抽象集合,包含一套schema component: 主要是元素与属性的声明、复杂与简单数据类型的定义。这些schema component通常是在处理一批schema document时被创建。schema文档包含着schema component的源语言定义。在日常使用中,一个schema文档常被称作一个schema。

Schema文档通过命名空间组织起来:所有的被命名的schema component属于一个目标命名空间;这个目标命名空间是schema文档作为整体的一个属性。schema文档可以包含进来(include)使用同一命名空间的其它schema文档,也可以导入(import)使用不同命名空间的schema文档。

当一个实例文档针对一个schema来验证有效性时(这一过程称为assessment),用来验证有效性的schema可以作为参数提供过验证器,也可以在实例文档中作为两种特殊属性之一直接提供:

  • xsi:schemaLocation
  • xsi:noNamespaceSchemaLocation.这种机制要求客户启动验证以充分相信这个文档,知道文档对正确的schema是有效的。

"xsi"是命名空间"http://www.w3.org/2001/XMLSchema-instance"的传统前缀。

XML Schema Documents通常有文件扩展名".xsd". XSD还没有专门的互联网媒体类型,因此按照 RFC 3023使用"application/xml"或"text/xml" .

Schema component

主要的schema component有:

  • 元素声明(Element declaration), 定义了元素的性质。包括:元素名字、目标命名空间;一个非常重要的性质是元素的类型,它限制了元素包含哪些属性与子元素。在XSD 1.1标准中,可以根据属性的值来有条件定义元素类型。一个元素可以属于一个替换群(substitution group),如果元素E在元素H的替换群中,那么schema许可H出现的地方E都可以出现。元素可以有完整性(integrity)约束:唯一性(uniqueness)约束确定特定值在该元素为根的子树中是独一无二的;引用(referential)约束确定值必须匹配一些其它元素的标识符。元素声明可以是全局的或局部的,允许同一个名字被用于一个实例文档的不同部分的不相关的元素。
  • 属性声明(Attribute declaration),定义了属性的性质。包括:属性名字、目标命名空间,属性类型限制了属性可以取哪些值,也可以指出属性的缺省值或固定值(fixed value,即属性只能取这个值)。
  • 简单与复杂数据类型(Simple and complex type).详见下节
  • 模型群(model group)与属性群(attribute group)定义。这实际上是宏(macro):被命名的元素的群与属性的群,可在许多数据类型定义中被重用。
  • 属性使用(attribute use)表示复杂数据类型与属性声明的关系,指出属性是必需的还是可选的,在什么时候使用这种数据类型。
  • 元素粒子(element particle)类似于表示复杂类型与元素的关系,指出元素在上下文中出现的最大与最小次数。类似于元素粒子,内容模型可以包括模型群粒子,在语法上相当于非终结符:定义了允许的元素序列的选择与重复的单位。此外,通配符粒子表示了一套元素或元素序列。

其它更专门的schema component包括annotations, assertions, notations, 以及包含了schema整体信息的schema component.

数据类型

简单数据类型(simple type)包含了可以出现在元素或属性的文本值。这是XSD与DTD的最大区别。

XSD提供了一套19个基本数据类型:

  • anyURI
  • base64Binary
  • boolean
  • date
  • dateTime
  • decimal
  • double
  • duration
  • float
  • hexBinary
  • gDay
  • gMonth
  • gMonthDay
  • gYear
  • gYearMonth
  • NOTATION
  • QName
  • string
  • time).

可以从这些基本数据类型通过三种机制构建三种数据类型:

  • restriction (减少值集的范围),
  • list (允许一个值的序列),
  • union (允许从几个数据类型中选择值).

XSD规范定义了25个导出数据类型。用户可以在schema中进一步定义自己的导出类型。

Restriction机制包括指出最大最小值、正则表达式、限制字符串的长度、限制十进制数的位数等。XSD 1.1又增加了assertions, 即通过一个[XPath 2.0]]表达式给出任意约束的能力。

复杂数据类型描述了一个元素的许可内容。包括这个元素、属性、子元素的许可内容。复杂类型定义由一套属性使用与一个内容模型组成。内容模型可以是:

  • 只有元素的内容(element-only content), 不允许有文本(但可以有空白符或者子元素可以有文本);
  • 简单内容(simple content), 允许有文本,不允许有子元素;
  • 空内容(empty content), 文本与子元素都不被允许;
  • 混合内容(mixed content), 文本与子元素都可以有.

复杂数据类型可以从别的复杂类型导出:

  • restriction方法,不允许基类型允许的一些元素、属性或者值
  • extension方法,允许额外的属性或元素出现。

XSD 1.1又增加了assertion方法来约束复杂类型, 即通过一个[XPath 2.0]]表达式必须求值为真

後Schema驗證資訊集(Post-Schema-Validation Infoset)

經過XML Schema為基的驗證後,可以依照驗證所隐含的資料模型来表達文档的结构與內容。XML Schema資料模型包括:

  • 字彙(元素與屬性名稱集)
  • 內容模型(關聯與結構)
  • 数据类型

這些訊息集成又叫後Schema驗證資訊集(Post-Schema-Validation Infoset (PSVI))。PSVI賦予合法XML文件它的"型別"並促進以物件般處理文件,如使用面向对象程序设计(OOP)變化型般操作。

這種對XML資料存取的特別的面向对象程序设计實現主要為微軟──對XML Schema發展的主要貢獻者──所倡導。轉換一份XML文件到自行資料型別感知物件在某些軟體設計部份相當有利。然而批評家爭論這同時暗中破壞了開放性──XML的主要特徵──並且它偏向於兼容原生於微軟偏好的編程語言的資源類型[3]

另,從XML Schema資料型別繼承出去的(以及肇因於XML Schema資料型別的)限制、這些資料型別與其他XML Schema間受限的搭配、以及在其他W3C規格裡這些資料型別的相依性,是許多XML軟體發展師的爭論焦點[4]

XML Schema的次要用途

XML Schema的主要用途是形式描述XML文档,然而最终的schema除了简单验证文档外还有许多其他用途。

代码生成

Schema可用于生成代码,这称作{[tsl|en|XML Data Binding}}。这些代码允许XML文档的内容作为编程环境中的对象。

XML文件结构文档的生成

Schema可用于产生人可读的文档来描述一个XML文件的结构。这在作者利用了标记元素(annotation element)时非常有用。

Criticism

Although XML Schema is successful in that it has been widely adopted and largely achieves what it set out to, it has been the subject of a great deal of severe criticism, perhaps more so than any other W3C Recommendation. Good summaries of the criticisms are provided by James Clark,[1] Anders Møller and Michael Schwartzbach,[2] Rick Jelliffe[3] and David Webber.[4]

General problems:

  • It is too complicated (the spec is several hundred pages in a very technical language), so it is hard to use by non-experts – but many non-experts need schemas to describe data formats. The W3C Recommendation itself is extremely difficult to read. Most users find W3Cs XML Schema Primer much easier to understand.
  • XSD lacks any formal mathematical specification. (This makes it difficult to reason about schemas, for example to prove that a modification to a schema is backwards compatible.)
  • There are many surprises in the language, for example that restriction of elements works differently from restriction of attributes.

Practical limitations of expressibility:

  • XSD offers very weak support for unordered content.
  • XSD cannot require a specific root element (so extra information is required to validate even the simplest documents).
  • When describing mixed content, the character data cannot be constrained in any way (not even a set of valid characters can be specified).
  • Content and attribute declarations cannot depend on attributes or element context (this was also listed as a central problem of DTD).
  • It is not 100% self-describing (as a trivial example, see the previous point), even though that was an initial design requirement.
  • Defaults cannot be specified separately from the declarations (this makes it hard to make families of schemas that only differ in the default values); element defaults can only be character data (not containing markup).

Technical problems:

  • Although it technically is namespace-conformant, it does not seem to follow the namespace spirit (e.g. "unqualified locals").
  • XSD 1.0 provided no facilities to state that the value or presence of one attribute is dependent on the values or presence of other attributes (so-called co-occurrence constraints). This has been fixed in XSD 1.1.
  • The set of XSD datatypes on offer is highly arbitrary.[5]
  • The two tasks of validation and augmentation (adding type information and default values) should be kept separate.

Version 1.1

XSD 1.1 became a W3C Recommendation in April 2012, which means it is an approved W3C specification.

Significant new features in XSD 1.1 are:

  • The ability to define assertions against the document content by means of XPath 2.0 expressions (an idea borrowed from Schematron).
  • The ability to select the type against which an element will be validated based on the values of the element's attributes ("conditional type assignment").
  • Relaxing the rules whereby explicit elements in a content model must not match wildcards also allowed by the model.
  • The ability to specify wildcards (for both elements and attributes) that apply to all types in the schema, so that they all implement the same extensibility policy.

Until the Proposed Recommendation draft, XSD 1.1 also proposed the addition of a new numeric data type, precisionDecimal. This proved controversial, and was therefore dropped from the specification at a late stage of development.


示范

一个Schema的简易示例,描述某个指定的国家,是这样的:

<xs:schema
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <xs:element name="country" type="Country">
     <xs:complexType name="Country">
      <xs:sequence>
       <xs:element name="name" type="xs:string"/>
       <xs:element name="population" type="xs:decimal"/>
      </xs:sequence>
     </xs:complexType>
   </xs:element>
</xs:schema>

一份遵从这个视图的XML文件:

<country
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="country.xsd">
  <name>France</name>
  <population>59.7</population>
</country>

參見

  • RELAX NG - 另一種XML綱要語言(ISO國際標準)通常用在XML Schema資料型別上
  • XML信息集

外部連結

  1. ^ James Clark summary of XML Schema criticisms, and promotion of RELAX NG as an alternative, https://web.archive.org/web/20150316212413/http://www.imc.org/ietf-xml-use/mail-archive/msg00217.html
  2. ^ Anders Møller and Michael I. Schwartzbach presents "Problems with XML Schema", http://cs.au.dk/~amoeller/XML/schemas/xmlschema-problems.html
  3. ^ Rick Jelliffe critique in May 2009, http://broadcast.oreilly.com/2009/05/w3c-please-put-xsd-11-on-hold.html
  4. ^ David Webber OASIS comparison and insights white paper from August 2008, http://www.oasis-open.org/committees/download.php/29164/White%20Paper%20on%20CAM%20and%20XSD.pdf
  5. ^ This point is amplified by Uche Ogbuji More on XML class warfare - O'Reilly ONLamp Blog