Xml 简明教程

XML - Schemas

XML Schema 通常称为 XML Schema Definition (XSD) 。它用于描述和验证 XML 数据的结构和内容。XML 模式定义元素、属性和数据类型。模式元素支持命名空间。它类似于描述数据库中数据的数据库模式。

XML Schema is commonly known as XML Schema Definition (XSD). It is used to describe and validate the structure and the content of XML data. XML schema defines the elements, attributes and data types. Schema element supports Namespaces. It is similar to a database schema that describes the data in a database.

Syntax

您需要在 XML 文档中声明模式,如下所示 −

You need to declare a schema in your XML document as follows −

Example

以下示例展示如何使用模式 −

The following example shows how to use schema −

<?xml version = "1.0" encoding = "UTF-8"?>
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema">
   <xs:element name = "contact">
      <xs:complexType>
         <xs:sequence>
            <xs:element name = "name" type = "xs:string" />
            <xs:element name = "company" type = "xs:string" />
            <xs:element name = "phone" type = "xs:int" />
         </xs:sequence>
      </xs:complexType>
   </xs:element>
</xs:schema>

XML 模式背后的基本思想是它们描述了 XML 文档可以采用的合法格式。

The basic idea behind XML Schemas is that they describe the legitimate format that an XML document can take.

Elements

正如我们在 XML - Elements 章节中所看到的,元素是 XML 文档的构建块。可以在 XSD 中如下定义元素 −

As we saw in the XML - Elements chapter, elements are the building blocks of XML document. An element can be defined within an XSD as follows −

<xs:element name = "x" type = "y"/>

Definition Types

您可以通过以下方式定义 XML 模式元素 −

You can define XML schema elements in the following ways −

Simple Type

简单类型元素仅在文本上下文中使用。一些预定义的简单类型是:xs:integer、xs:boolean、xs:string、xs:date。例如 −

Simple type element is used only in the context of the text. Some of the predefined simple types are: xs:integer, xs:boolean, xs:string, xs:date. For example −

<xs:element name = "phone_number" type = "xs:int" />

Complex Type

复杂类型是其他元素定义的容器。这允许您指定哪些子元素一个元素可以包含,并在 XML 文档中提供一些结构。例如 −

A complex type is a container for other element definitions. This allows you to specify which child elements an element can contain and to provide some structure within your XML documents. For example −

<xs:element name = "Address">
   <xs:complexType>
      <xs:sequence>
         <xs:element name = "name" type = "xs:string" />
         <xs:element name = "company" type = "xs:string" />
         <xs:element name = "phone" type = "xs:int" />
      </xs:sequence>
   </xs:complexType>
</xs:element>

在上面的示例中,Address 元素由子元素组成。这是其他 <xs:element> 定义的容器,允许在 XML 文档中构建一个简单的元素层次结构。

In the above example, Address element consists of child elements. This is a container for other <xs:element> definitions, that allows to build a simple hierarchy of elements in the XML document.

Global Types

通过全局类型,您可以在文档中定义一个单一的类型,所有其他引用都可以使用它。例如,假设您想要对公司的不同地址对人和公司进行常规化。在这样的情况下,您可以按如下方式定义一个常规类型 −

With the global type, you can define a single type in your document, which can be used by all other references. For example, suppose you want to generalize the person and company for different addresses of the company. In such case, you can define a general type as follows −

<xs:element name = "AddressType">
   <xs:complexType>
      <xs:sequence>
         <xs:element name = "name" type = "xs:string" />
         <xs:element name = "company" type = "xs:string" />
      </xs:sequence>
   </xs:complexType>
</xs:element>

现在让我们在我们的示例中按如下方式使用这种类型 −

Now let us use this type in our example as follows −

<xs:element name = "Address1">
   <xs:complexType>
      <xs:sequence>
         <xs:element name = "address" type = "AddressType" />
         <xs:element name = "phone1" type = "xs:int" />
      </xs:sequence>
   </xs:complexType>
</xs:element>

<xs:element name = "Address2">
   <xs:complexType>
      <xs:sequence>
         <xs:element name = "address" type = "AddressType" />
         <xs:element name = "phone2" type = "xs:int" />
      </xs:sequence>
   </xs:complexType>
</xs:element>

无需分别为 Address1 和 Address2 定义名称和公司两次,我们现在只需进行一次定义。这使得维护更简单,即,如果您决定向地址添加“邮政编码”元素,则只需在某一位置添加它们即可。

Instead of having to define the name and the company twice (once for Address1 and once for Address2), we now have a single definition. This makes maintenance simpler, i.e., if you decide to add "Postcode" elements to the address, you need to add them at just one place.

Attributes

XSD 中的属性在元素中提供额外信息。属性具有如下所示的名称和类型属性 −

Attributes in XSD provide extra information within an element. Attributes have name and type property as shown below −

<xs:attribute name = "x" type = "y"/>