XSD(Schema)教程 [转]
schema教程
XML Schema是以XML语言为基础的,它用于可替代DTD。
一份XML schema文件描述了XML文档的结构
XML Schema语言也被称为XML Schema Definition (XSD)(XML Schema定义).
在本章教程你将学习如何读取和建立XML Schemas,以及为什么XML Schemas比DTDs的功能更为强大,还有如何在你的程序中使用XML Schema语言
XML Schema 参考资料
你可以找到完整的有效的XML Schema元素
XML Schema is an XML-based alternative to DTD.
XML Schema是以XML语言为基础的,它用于可替代DTD。
An XML schema describes the structure of an XML document.
一份XML schema文件描述了XML文档的结构。
The XML Schema language is also referred to as XML Schema Definition (XSD).
XML Schema语言也被称为XML Schema Definition (XSD)(XML Schema定义).
What You Should Already Know
你应该已经掌握的知识
Before you continue you should have a basic understanding of the following:
在继续之前你应对以下内容有基本了解:
- HTML / XHTML
- XML and XML Namespaces
XML 和 XML 名称空间 - A basic understanding of DTD
对DTD有基本的了解
If you want to study these subjects first, find the tutorials on our Home page.
如果你想先学这些内容,请看我们主页上的的学习教程
What is an XML Schema?
什么是XML Schema?
The purpose of an XML Schema is to define the legal building blocks of an XML document, just like a DTD.
XML Schema的作用是定义一份XML文档的合法组件群,就像DTD的作用一样
An XML Schema:
一份XML Schema
- defines elements that can appear in a document
定义了可以出现在文档里的元素 - defines attributes that can appear in a document
定义了可以出现在文档里的属性 - defines which elements are child elements
定义了哪些元素是子元素 - defines the order of child elements
定义了子元素的顺序 - defines the number of child elements
定义了子元素的数量 - defines whether an element is empty or can include text
定义了一个元素应是否能包含文本,或应该是空的 - defines data types for elements and attributes
定义了元素和属性的数据类型 - defines default and fixed values for elements and attributes
定义了元素和属性的默认值和固定值
XML Schemas are the Successors of DTDs
XML Schema 是DTD的“接任者”
We think that very soon XML Schemas will be used in most Web applications as a replacement for DTDs. Here are some reasons:
我们认为XML Schema很快就会替代DTD在大多数的网络应用程序中被广泛使用。
- XML Schemas are extensible to future additions
XML Schema针对将来的额外内容是可扩展的 - XML Schemas are richer and more powerful than DTDs
XML Schema内容比DTD丰富,作用也更大 - XML Schemas are written in XML
XML Schema是以XML语言编写而成的 - XML Schemas support data types
XML Schema支持数据类型 - XML Schemas support namespaces
XML Schema支持名称空间(namespaces)
XML Schema is a W3C Standard
XML Schema是W3C定义的标准
XML Schema became a W3C Recommendation 02. May 2001.
XML Schema于2001年5月2日成为W3C推荐使用的标准
You can read more about the XML Schema standard in our W3C tutorial.
在W3C 教程中你能阅读到更多关于XML Schema标准。
XML Schemas are much more powerful than DTDs.
XML Schema比DTD作用更加强大。
XML Schemas Support Data Types
XML Schema支持数据类型
One of the greatest strength of XML Schemas is the support for data types.
XML Schema的最好用的地方之一就是它支持数据类型
With support for data types:
支持数据类型所带来的好处:
- It is easier to describe allowable document content
更易于描述被允许的文档内容 - It is easier to validate the correctness of data
更易于检验数据的正确性 - It is easier to work with data from a database
更易于与数据库中的数据一起协同工作 - It is easier to define data facets (restrictions on data)
更易于定义数据的使用面(关于数据的限制) - It is easier to define data patterns (data formats)
更易于定义数据样式(数据格式) - It is easier to convert data between different data types
更易于把数据转换成不同的数据类型
XML Schemas use XML Syntax
XML Schema使用XML的语法
Another great strength about XML Schemas is that they are written in XML.
XML Schema另一个好处就是他们是用XML编写成的。
Some benefits of that XML Schemas are written in XML:
用XML编写的好处是:
- You don't have to learn a new language
可以不需要再学一种新语言 - You can use your XML editor to edit your Schema files
可以用XML编辑器来编辑Schema文件 - You can use your XML parser to parse your Schema files
可以用XML解析器解析Schema文件 - You can manipulate your Schema with the XML DOM
可以用XML DOM 处理Schema - You can transform your Schema with XSLT
可以用XSLT转换Schema
XML Schemas Secure Data Communication
XML Schema安全数据通讯
When sending data from a sender to a receiver, it is essential that both parts have the same "expectations" about the content.
当数据由发送者被传向接受者时,双方对“数据内容理解的一致性”就显得非常重要了
With XML Schemas, the sender can describe the data in a way that the receiver will understand.
在XML Schema里,发送者可以用接受者能够理解的方式描述数据。
A date like: "03-11-2004" will, in some countries, be interpreted as 3.November and in other countries as 11.March.
像这样格式的日期:"03-11-2004",一些国家会解释成11月3日,而别的国家会解释成3月11日
However, an XML element with a data type like this:
<date type="date">2004-03-11</date>
ensures a mutual understanding of the content, because the XML data type "date" requires the format "YYYY-MM-DD".
然而,一个有着像这样的数据种类的XML元素:<date type="date">2004-03-11</date>,就确保了双方都能理解其内容,因为XML数据类型的"date"要求规定为"YYYY-MM-DD"的格式。
XML Schemas are Extensible
XML Schema是可扩展的
XML Schemas are extensible, because they are written in XML.
因为XML Schema文件是由XML编写的,所以它们是可扩展的。
With an extensible Schema definition you can:
Schema可扩展意味着你可以:
- Reuse your Schema in other Schemas
在别的Schema文件里再次用到你的Schema - Create your own data types derived from the standard types
从标准的数据类型中派生出你自己的数据类型 - Reference multiple schemas in the same document
在相同的文档中参考多种Schema。
Well-Formed is not Enough
光有“规范”仍然是不够的
A well-formed XML document is a document that conforms to the XML syntax rules, like:
规范的XML文档是符合XML语法规则的文档,就像这样:
- it must begin with the XML declaration
• 开头必须有XML声明 - it must have one unique root element
• 必须有一个独特的根目录元素 - start-tags must have matching end-tags
• 开始标签(start-tag)必须和结束标签(end-tag)匹配 - elements are case sensitive
• 元素是有大小写区分的 - all elements must be closed
• 元素必须有结束 - all elements must be properly nested
• 所有的元素必须被合理嵌套 - all attribute values must be quoted
• 所有属性值必须写在引号里 - entities must be used for special characters
• 用特殊字符时必须用到实体
Even if documents are well-formed they can still contain errors, and those errors can have serious consequences.
即使文档是规范的,它们也可能包含错误。这些错误可能会引起严重的后果。
Think of the following situation: you order 5 gross of laser printers, instead of 5 laser printers. With XML Schemas, most of these errors can be caught by your validating software.
试想下面的情况:你预定了 5罗(1罗=12打=144台)激光打印机,而不是5台打印机。在XML Schema文件里,大多数像这样的错误会由检验软件产生。
XML documents can have a reference to a DTD or to an XML Schema.
XML文档能和一份DTD或XML Schema文件相关联。
A Simple XML Document
一份简单的XML文档
Look at this simple XML document called "note.xml":
请看下面名为"note.xml"的一份简易的XML文档
<?xml version="1.0"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> |
A DTD File
一份DTD文件
The following example is a DTD file called "note.dtd" that defines the elements of the XML document above ("note.xml"):
下面的例子是名为"note.dtd"的DTD文件,它定义了上述XML文档("note.xml")的元素。
<!ELEMENT note (to, from, heading, body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)> |
The first line defines the note element to have four child elements: "to, from, heading, body".
第一行定义了含有"to, from, heading, body".四个子元素的“note”元素。
Line 2-5 defines the to, from, heading, body elements to be of type "#PCDATA".
第2到5行定义了"#PCDATA". 类型的“to, from, heading, body”四个元素
An XML Schema
一份XML Schema
The following example is an XML Schema file called "note.xsd" that defines the elements of the XML document above ("note.xml"):
下面的例子当中,一个名为"note.xsd"的XML Schema文件定义了上述XML文档("note.xml")中的元素
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com" elementFormDefault="qualified"> <xs:element name="note"> <xs:complexType> <xs:sequence> <xs:element name="to" type="xs:string"/> <xs:element name="from" type="xs:string"/> <xs:element name="heading" type="xs:string"/> <xs:element name="body" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> |
The note element is a complex type because it contains other elements. The other elements (to, from, heading, body) are simple types because they do not contain other elements. You will learn more about simple and complex types in the following chapters.
Note元素是复合类型(complex type)因为它包含了其他元素,其他元素(to, from, heading, body)属于简单类型(simple type),因为它不包含其他元素。在下面几章里你会学到更多关于简单类型和复合类型的相关知识。
A Reference to a DTD
和DTD相关
This XML document has a reference to a DTD:
XML文档和DTD相关:
<?xml version="1.0"?> <!DOCTYPE note SYSTEM "http://www.w3schools.com/dtd/note.dtd"> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> |
A Reference to an XML Schema
和XML Schema相关
This XML document has a reference to an XML Schema:
下面的XML文档和XML Schema相关:
<?xml version="1.0"?> <note xmlns="http://www.w3schools.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com note.xsd"> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> |
The <schema> element is the root element of every XML Schema.
<schema>元素是每个XML Schema文件的根元素。
The <schema> Element
<schema>元素
The <schema> element is the root element of every XML Schema:
<schema>元素是每份XML Schema文件的根元素。
<?xml version="1.0"?> <xs:schema> ... ... </xs:schema> |
The <schema> element may contain some attributes. A schema declaration often looks something like this:
<schema>元素也可以含有一些属性,一个schema声明经常写成这样:
<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com" elementFormDefault="qualified"> ... ... </xs:schema> |
The following fragment:
看下面的片段:
xmlns:xs="http://www.w3.org/2001/XMLSchema" |
indicates that the elements and data types used in the schema come from the "http://www.w3.org/2001/XMLSchema" namespace. It also specifies that the elements and data types that come from the "http://www.w3.org/2001/XMLSchema" namespace should be prefixed with xs:
指明了在schema中使用的元素和数据种类来自http://www.w3.org/2001/XMLSchema名称空间(namespace)。它也指定了来自"http://www.w3.org/2001/XMLSchema"名称空间(namespace)的元素和数据种类必须带前缀“xs:”
This fragment:
这个片段:
targetNamespace="http://www.w3schools.com" |
indicates that the elements defined by this schema (note, to, from, heading, body.) come from the "http://www.w3schools.com" namespace.
暗示了由这份schema(note, to, from, heading, body.)定义的元素来自"http://www.w3schools.com"名称空间(namespace)
This fragment:
这个片段:
xmlns="http://www.w3schools.com" |
indicates that the default namespace is http://www.w3schools.com.
指明了默认名称空间(namespace)是http://www.w3schools.com.
This fragment:
这个片段
elementFormDefault="qualified" |
indicates that any elements used by the XML instance document which were declared in this schema must be namespace qualified.
指明了由这份schema声明的XML实例文档里用到的任何元素,都必须是有效的名称空间(namespace qualified)。
Referencing a Schema in an XML Document
在一份XML文档里提到Schema
This XML document has a reference to an XML Schema:
一份XML文档里提提到XML Schiema
<?xml version="1.0"?> <note xmlns="http://www.w3schools.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3schools.com note.xsd"> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> |
The following fragment:
下面的片段:
xmlns="http://www.w3schools.com" |
specifies the default namespace declaration. This declaration tells the schema-validator that all the elements used in this XML document are declared in the "http://www.w3schools.com" namespace.
指定了默认的名称空间(default namespace)声明。这个声明告诉schema-检验器:这份XML文档里用到的所有元素都在http://www.w3schools.com的名称空间(namespace)中声明过。
Once you have the XML Schema Instance namespace available:
一旦你有了可以利用的XML Schema Instance的名称空间(namespace):
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
you can use the schemaLocation attribute. This attribute has two values. The first value is the namespace to use. The second value is the location of the XML schema to use for that namespace:
你可以用到SchemaLocation属性。这个属性有两个值。第一个值是要用到的名称空间(namespace)。第二个值是为名称空间(namespace)指定了需要使用的XML schema的位置。
xsi:schemaLocation="http://www.w3schools.com note.xsd" |
XML Schemas define the elements of your XML files.
XML Schemas(XML公式)定义了XML文件的元素
A simple element is an XML element that contains only text. It cannot contain any other elements or attributes.
简单元素是只含有文本的XML元素。它不可以含有其他元素或属性
What is a Simple Element?
什么是简单元素?
A simple element is an XML element that can contain only text. It cannot contain any other elements or attributes.
简单元素是只含有文本的XML元素。它不可以含有其他元件或属性
However, the "only text" restriction is quite misleading. The text can be of many different types. It can be one of the types included in the XML Schema definition (boolean, string, date, etc.), or it can be a custom type that you can define yourself.
但是,“只含文本”这个限定条件是非常容易引起误解的,文本可以有很多的不同类型。它可以是XML Schema定义里的文本类型之一(逻辑值,字符串,日期,等等),也可以是自定义文本类型。
You can also add restrictions (facets) to a data type in order to limit its content, or you can require the data to match a specific pattern.
你可以通过给数据类型添加限定条件来限制它的内容,或者你可以要求数据与指定的式样相匹配。
Defining a Simple Element
定义一个简明元素
The syntax for defining a simple element is:
定义简明元素的语法为:
<xs:element name="xxx" type="yyy"/> |
where xxx is the name of the element and yyy is the data type of the element.
在这里, xxx是元素名称,yyy是元素的数据类型
XML Schema has a lot of built-in data types. The most common types are:
XML Schema本身有很多数据种类。最常见的种类有:
- xs:string
xs:字符 - xs:decimal
xs: 小数 - xs:integer
xs:整数 - xs:boolean
xs:逻辑值 - xs:date
xs:日期 - xs:time
xs:时间
Example
例子
Here are some XML elements:
这里是一些XML元素
<lastname>Refsnes</lastname> <age>36</age> <dateborn>1970-03-27</dateborn> |
And here are the corresponding simple element definitions:
下面是相应的简单元素定义
<xs:element name="lastname" type="xs:string"/> <xs:element name="age" type="xs:integer"/> <xs:element name="dateborn" type="xs:date"/> |
Default and Fixed Values for Simple Elements
简单元素的默认值和固定值
Simple elements may have a default value OR a fixed value specified.
简单元素也许会有指定的默认值或固定值
A default value is automatically assigned to the element when no other value is specified.
值没有被指定时,属性自动会设置成默认值
In the following example the default value is "red":
下面例子里默认值是"red":
<xs:element name="color" type="xs:string" default="red"/> |
A fixed value is also automatically assigned to the element, and you cannot specify another value.
固定值是也是自动分派给属性的,并且,一旦有了固定值,你就不能指定其他值了。
In the following example the fixed value is "red":
下面例子里固定值是"red":
<xs:element name="color" type="xs:string" fixed="red"/> |
All attributes are declared as simple types.
所有属性都是以简单类型来声明的。
What is an Attribute?
属性是什么?
Simple elements cannot have attributes. If an element has attributes, it is considered to be of a complex type. But the attribute itself is always declared as a simple type.
简单的元素没有属性。当元素含有属性时,它会被认为是复合元素。但属性本身总是被声明为简单类型。
How to Define an Attribute?
怎么样定义属性?
The syntax for defining an attribute is:
定义一项属性的语法是:
<xs:attribute name="xxx" type="yyy"/> |
where xxx is the name of the attribute and yyy specifies the data type of the attribute.
其中,xxx是属性的名称,yyy指定了属性的数据类型
XML Schema has a lot of built-in data types. The most common types are:
XML Schema本身有很多数据类型。最常见的数据类型有:
- xs:string
xs:字符串 - xs:decimal
xs:小数 - xs:integer
xs:整数 - xs:boolean
xs:逻辑值 - xs:date
xs:日期 - xs:time
xs:时间
Example
例子
Here is an XML element with an attribute:
这是一个有属性的XML元素:
<lastname lang="EN">Smith</lastname> |
And here is the corresponding attribute definition:
而这个相应的属性定义:
<xs:attribute name="lang" type="xs:string"/> |
Default and Fixed Values for Attributes
属性的默认值和固定值
Attributes may have a default value OR a fixed value specified.
属性也许有指定的默认值或固定值
A default value is automatically assigned to the attribute when no other value is specified.
值没有被指定时,属性自动会设成默认值
In the following example the default value is "EN":
下面例子里默认值是"EN":
<xs:attribute name="lang" type="xs:string" default="EN"/> |
A fixed value is also automatically assigned to the attribute, and you cannot specify another value.
固定值是也是自动分派给属性的,并且,一旦有了固定值,你就不能指定其他值了。
In the following example the fixed value is "EN":
下面例子里固定值是"EN":
<xs:attribute name="lang" type="xs:string" fixed="EN"/> |
Optional and Required Attributes
任意属性和必需属性
Attributes are optional by default. To specify that the attribute is required, use the "use" attribute:
默认时属性是任意的,要指明属性是必需的,须用到"use"属性
<xs:attribute name="lang" type="xs:string" use="required"/> |
Restrictions on Content
对内容的约束
When an XML element or attribute has a data type defined, it puts restrictions on the element's or attribute's content.
当XML元素或属性有了已定义的数据类型,元素或属性的内容会有约束
If an XML element is of type "xs:date" and contains a string like "Hello World", the element will not validate.
如果一个XML元素是"xs:date"类型,并包含着象"Hello World"的字符串,元素就不会进行检验
With XML Schemas, you can also add your own restrictions to your XML elements and attributes. These restrictions are called facets. You can read more about facets in the next chapter.
你也可以用XML Schema给XML元素和属性添加约束限制。这些约束称为“面(facet)”。在下章里你可以读到更多关于“面”的内容
Restrictions are used to define acceptable values for XML elements or attributes. Restrictions on XML elements are called facets.
约束用于给XML元素或属性定义可接受的值,关于对XML元素的约束称之为“面(facet)”
Restrictions on Values
对单个值的约束
The following example defines an element called "age" with a restriction. The value of age cannot be lower than 0 or greater than 120:
下面的例子给叫做"age"的元件定义了一个“约束(restriction)”。“age”的值要大等于0,小等于120:
<xs:element name="age"> <xs:simpleType> <xs:restriction base="xs:integer"> <xs:minInclusive value="0"/> <xs:maxInclusive value="120"/> </xs:restriction> </xs:simpleType> </xs:element> |
Restrictions on a Set of Values
对一组值的约束
To limit the content of an XML element to a set of acceptable values, we would use the enumeration constraint.
为了限制XML元素的内容得到一组符合条件的值,我们会用到“列举约束(enumeration constraint)”。
The example below defines an element called "car" with a restriction. The only acceptable values are: Audi, Golf, BMW:
下面的例子给叫做"car"的元素定义了约束条件,符合条件的值有:Audi, Golf, BMW:
<xs:element name="car"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="Audi"/> <xs:enumeration value="Golf"/> <xs:enumeration value="BMW"/> </xs:restriction> </xs:simpleType> </xs:element> |
The example above could also have been written like this:
上面的例子也可以写成这样:
<xs:element name="car" type="carType"/> <xs:simpleType name="carType"> <xs:restriction base="xs:string"> <xs:enumeration value="Audi"/> <xs:enumeration value="Golf"/> <xs:enumeration value="BMW"/> </xs:restriction> </xs:simpleType> |
Note: In this case the type "carType" can be used by other elements because it is not a part of the "car" element.
注意:在这种情况下"carType"类型可以被其他元件所使用,因为它不是"car"元素的一部分
Restrictions on a Series of Values
对一系列值的约束
To limit the content of an XML element to define a series of numbers or letters that can be used, we would use the pattern constraint.
为了限制XML元件的内容以定义一系列可被使用的数字或字母,我们可以用“式样约束(pattern constraints)”。
The example below defines an element called "letter" with a restriction. The only acceptable value is ONE of the LOWERCASE letters from a to z:
下面的例子给叫做"letter"的元素定义可约束。唯一符合条件的值是 a到z之间的一个小写字母:
<xs:element name="letter"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[a-z]"/> </xs:restriction> </xs:simpleType> </xs:element> |
The next example defines an element called "initials" with a restriction. The only acceptable value is THREE of the UPPERCASE letters from a to z:
接下来的例子给叫做"initials"的元素定义了一个约束。唯一符合条件的值是a到z之间的3个大写字母
<xs:element name="initials"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[A-Z][A-Z][A-Z]"/> </xs:restriction> </xs:simpleType> </xs:element> |
The next example also defines an element called "initials" with a restriction. The only acceptable value is THREE of the LOWERCASE OR UPPERCASE letters from a to z:
下面的例子给叫做"initials"的元素定义了一个约束。唯一符合条件的值是 a到z之间的三个大写或小写字母
<xs:element name="initials"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/> </xs:restriction> </xs:simpleType> </xs:element> |
The next example defines an element called "choice" with a restriction. The only acceptable value is ONE of the following letters: x, y, OR z:
下面的例子给叫做"choice"的元素定义了一个约束,唯一符合条件的值是x,y,z三个字母中的任意一个
<xs:element name="choice"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[xyz]"/> </xs:restriction> </xs:simpleType> </xs:element> |
The next example defines an element called "prodid" with a restriction. The only acceptable value is FIVE digits in a sequence, and each digit must be in a range from 0 to 9:
下面的例子给叫做"prodid"的元素定义了一个约束,唯一符合条件的值是0到9的5个阿拉伯数字的排列,
<xs:element name="prodid"> <xs:simpleType> <xs:restriction base="xs:integer"> <xs:pattern value="[0-9][0-9][0-9][0-9][0-9]"/> </xs:restriction> </xs:simpleType> </xs:element> |
Other Restrictions on a Series of Values
对一系列值的其他约束
The example below defines an element called "letter" with a restriction. The acceptable value is zero or more occurrences of lowercase letters from a to z:
下面的例子给叫做"letter"的元素定义了一个约束。唯一符合条件的值是a 到z的小写字母(可以有多个)或0
<xs:element name="letter"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="([a-z])*"/> </xs:restriction> </xs:simpleType> </xs:element> |
The next example also defines an element called "letter" with a restriction. The acceptable value is one or more pairs of letters, each pair consisting of a lower case letter followed by an upper case letter. For example, "sToP" will be validated by this pattern, but not "Stop" or "STOP" or "stop":
下面的例子也给叫做"letter"的元素定义了一个约束。唯一符合条件的值是一对或多对字母,每对都是一个小写字母后跟一个大写字母组成。举个例子,"sToP"在这种式样里是有效正确的,但"Stop" ,"STOP" 或 "stop"就都不是了。
<xs:element name="letter"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="([a-z][A-Z])+"/> </xs:restriction> </xs:simpleType> </xs:element> |
The next example defines an element called "gender" with a restriction. The only acceptable value is male OR female:
下面的例子也给叫做"gender"的元素定义了一个约束。唯一符合的值是male (男性)或female(女性):
<xs:element name="gender"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="male|female"/> </xs:restriction> </xs:simpleType> </xs:element> |
The next example defines an element called "password" with a restriction. There must be exactly eight characters in a row and those characters must be lowercase or uppercase letters from a to z, or a number from 0 to 9:
下面的例子也给叫做"password"的元素定义了一个约束。一行里必须有8个字符,字符必须是a到z大或小写字母,或者是0到9的数字
<xs:element name="password"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[a-zA-Z0-9]{8}"/> </xs:restriction> </xs:simpleType> </xs:element> |
Restrictions on Whitespace Characters
对空白符的约束
To specify how whitespace characters should be handled, we would use the whiteSpace constraint.
为了指定空白符该怎样被处理,我们可以用空白符约束
This example defines an element called "address" with a restriction. The whiteSpace constraint is set to "preserve", which means that the XML processor WILL NOT remove any white space characters:
下面的例子给叫做"address"的元素定义了一个约束。空白符设为"preserve"(保留),这意味着XML处理器不会删除任何空白字符:
<xs:element name="address"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:whiteSpace value="preserve"/> </xs:restriction> </xs:simpleType> </xs:element> |
This example also defines an element called "address" with a restriction. The whiteSpace constraint is set to "replace", which means that the XML processor WILL REPLACE all white space characters (line feeds, tabs, spaces, and carriage returns) with spaces:
下面的例子也给叫做"address"的元素定义了一个约束。空白符设为" replace "(替代),这意味着XML处理器会用空格替代所有的空白字符(换行符, 制表符, 空格符, 回车符))
<xs:element name="address"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:whiteSpace value="replace"/> </xs:restriction> </xs:simpleType> </xs:element> |
This example also defines an element called "address" with a restriction. The whiteSpace constraint is set to "collapse", which means that the XML processor WILL REMOVE all white space characters (line feeds, tabs, spaces, carriage returns are replaced with spaces, leading and trailing spaces are removed, and multiple spaces are reduced to a single space):
下面的例子也给叫做"address"的元素定义了一个约束。空白符设为"collapse"(消除),这意味着XML处理器会清除所有的空白字符(换行符, 制表符, 空格符以及回车符都被空格符代替。头尾空格会被清除,多个空格也会减少为一个)
<xs:element name="address"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:whiteSpace value="collapse"/> </xs:restriction> </xs:simpleType> </xs:element> |
Restrictions on Length
对长度的约束
To limit the length of a value in an element, we would use the length, maxLength, and minLength constraints.
为了限制元素的长度值,我们会用length, maxLength, 和 minLength 约束。
This example defines an element called "password" with a restriction. The value must be exactly eight characters:
下面的例子给叫做"password"的元素定义了一个约束。值必须正好有8个字符:
<xs:element name="password"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:length value="8"/> </xs:restriction> </xs:simpleType> </xs:element> |
This example defines another element called "password" with a restriction. The value must be minimum five characters and maximum eight characters:
下面的例子给叫做"password"的元素定义了一个约束。值最少要有5个字符,最多有8个字符。
<xs:element name="password"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:minLength value="5"/> <xs:maxLength value="8"/> </xs:restriction> </xs:simpleType> </xs:element> |
Restrictions for Datatypes
对数据类型的约束
Constraint 约束 |
Description 说明 |
---|---|
enumeration | Defines a list of acceptable values 定义了一系列的有效值 |
fractionDigits | Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero 指定了允许的小数位数的最多位数。必须大于等于0 |
length | Specifies the exact number of characters or list items allowed. Must be equal to or greater than zero 指定了允许的字符或列表项的个数。必须大于等于0 |
maxExclusive | Specifies the upper bounds for numeric values (the value must be less than this value) 指定了数值的上限(数值要比这个值小) |
maxInclusive | Specifies the upper bounds for numeric values (the value must be less than or equal to this value) 指定了数值上限(数值必须小于等于这个值) |
maxLength | Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero 指定了所允许的字符或列表项的最多个数。必须大于等于0 |
minExclusive | Specifies the lower bounds for numeric values (the value must be greater than this value) 指定了数值的下限 (数值要比这个值小) |
minInclusive | Specifies the lower bounds for numeric values (the value must be greater than or equal to this value) 指定了数值的下限(数值必须大于等于这个值) |
minLength | Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero 指定了所允许的字符或列表的最少个数。必须等于大于0个 |
pattern | Defines the exact sequence of characters that are acceptable 定义了符合要求的字符的确切排列顺序 |
totalDigits | Specifies the exact number of digits allowed. Must be greater than zero 指定了所允许的字符的确切个数。必须大于0 |
whiteSpace | Specifies how white space (line feeds, tabs, spaces, and carriage returns) is handled 指定了空白该怎样被处理(换行符,制表符,空格符和回车符) |
A complex element contains other elements and/or attributes.
一个复合元素(Complex Elements)包含其他元素和/或属性
What is a Complex Element?
什么是复合元素(Complex Elements)?
A complex element is an XML element that contains other elements and/or attributes.
复合元素(Complex Elements)是含有其他元素和/或属性的XML元素
There are four kinds of complex elements:
有四种复合元素(Complex Elements):
- empty elements
空元素 - elements that contain only other elements
只含有其他元素的元素 - elements that contain only text
只含有文本的元素 - elements that contain both other elements and text
含有文本和其他元素的元素
Note: Each of these elements may contain attributes as well!
注意:这些元素中的每一个也许还含有属性!
Examples of Complex Elements
复合元素(Complex Elements)的例子
A complex XML element, "product", which is empty:
一个空的复合XML元素"product":
<product pid="1345"/> |
A complex XML element, "employee", which contains only other elements:
只含有其他元素的复合XML元素, "employee"
<employee> <firstname>John</firstname> <lastname>Smith</lastname> </employee> |
A complex XML element, "food", which contains only text:
只含有文本的复合XML元素, "food":
<food type="dessert">Ice cream</food> |
A complex XML element, "description", which contains both elements and text:
含有元素和文本的复合XML元素, "description":
<description> It happened on <date lang="norwegian">03.03.99</date> .... </description> |
How to Define a Complex Element
怎样定义一个复合元素(Complex Elements)?
Look at this complex XML element, "employee", which contains only other elements:
看这个只含有其他元素的复合XML元素,"employee":
<employee> <firstname>John</firstname> <lastname>Smith</lastname> </employee> |
We can define a complex element in an XML Schema two different ways:
我们有两种方法可以在一篇XML Schema里定义一个复合元素(Complex Elements):
1. The "employee" element can be declared directly by naming the element, like this:
1. "employee"元素可以直接通过命名元素的方式被声明,像这样:
<xs:element name="employee"> <xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> |
If you use the method described above, only the "employee" element can use the specified complex type. Note that the child elements, "firstname" and "lastname", are surrounded by the <sequence> indicator. This means that the child elements must appear in the same order as they are declared. You will learn more about indicators in the XSD Indicators chapter.
如果你用了上面的方法,那么只有"employee"元素才可以用指定的复合类型。注意子元素"firstname" 和 "lastname",它们是被包围在<sequence>“指示器”元素里的。这意味着子元素必须以它们被声明的顺序出现。在XSD指示器这章里你可以学到关于指示器更多内容。
2. The "employee" element can have a type attribute that refers to the name of the complex type to use:
2. "employee"元素可以有个类型属性,其所指的是要用的复合类型的名称
<xs:element name="employee" type="personinfo"/> <xs:complexType name="personinfo"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType> |
If you use the method described above, several elements can refer to the same complex type, like this:
如果你用上述方法,几个元素指的可以是相同的复合类型,就像这样:
<xs:element name="employee" type="personinfo"/> <xs:element name="student" type="personinfo"/> <xs:element name="member" type="personinfo"/> <xs:complexType name="personinfo"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType> |
You can also base a complex element on an existing complex element and add some elements, like this:
你也可以在现存的复合元素(Complex Elements)上再加上一个复合元素(Complex Elements),并添加一些元素,就像这样:
<xs:element name="employee" type="fullpersoninfo"/> <xs:complexType name="personinfo"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType> <xs:complexType name="fullpersoninfo"> <xs:complexContent> <xs:extension base="personinfo"> <xs:sequence> <xs:element name="address" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="country" type="xs:string"/> </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType> |
An empty complex element cannot have contents, only attributes.
一个空的复合元素不能含有内容,只能含有属性。
Complex Empty Elements
复合空元素(Complex Empty Elements)
An empty XML element:
一个空的XML元素:
<product prodid="1345" /> |
The "product" element above has no content at all. To define a type with no content, we must define a type that allows only elements in its content, but we do not actually declare any elements, like this:
上述"product"元素完全不含内容。为定义不含内容的类型,我们必须定义一个内容中只允许出现元素的类型,但我们不需要声明任何元素,就像这样:
<xs:element name="product"> <xs:complexType> <xs:complexContent> <xs:restriction base="xs:integer"> <xs:attribute name="prodid" type="xs:positiveInteger"/> </xs:restriction> </xs:complexContent> </xs:complexType> </xs:element> |
In the example above, we define a complex type with a complex content. The complexContent element signals that we intend to restrict or extend the content model of a complex type, and the restriction of integer declares one attribute but does not introduce any element content.
上述例子中,我们定义了一个有复合内容的复合类型。复合内容的元素表示了我们想要约束或扩充的复合类型的内容模式。对整数的约束声明了一个属性,但并没有介绍任何元素内容。
However, it is possible to declare the "product" element more compactly, like this:
但是,可以更加简洁地声明"product"元素,就像这样:
<xs:element name="product"> <xs:complexType> <xs:attribute name="prodid" type="xs:positiveInteger"/> </xs:complexType> </xs:element> |
Or you can give the complexType element a name, and let the "product" element have a type attribute that refers to the name of the complexType (if you use this method, several elements can refer to the same complex type):
或者你可以给complexType元素起个名称,并让"product"元素有个类型属性,而且类型属性引用的是complexType的名称(如果你用这个方法,几个元素可以引用相同的复合类型):
<xs:element name="product" type="prodtype"/> <xs:complexType name="prodtype"> <xs:attribute name="prodid" type="xs:positiveInteger"/> </xs:complexType> |
An "elements-only" complex type contains an element that contains only other elements.
“只有元素(Elements-only)”复合类型含有一个只包含其他元素的元素
Complex Types Containing Elements Only
复合类型只含有元素
An XML element, "person", that contains only other elements:
一个XML元素, "person",只含有其他元素:
<person> <firstname>John</firstname> <lastname>Smith</lastname> </person> |
You can define the "person" element in a schema, like this:
你可以在一篇schema里定义"person"元素,就像这样
<xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> |
Notice the <xs:sequence> tag. It means that the elements defined ("firstname" and "lastname") must appear in that order inside a "person" element.
注意<xs:sequence>标签。这表示所定义的元素("firstname" 和 "lastname")必须在"person"元素里以那样的次序出现。
Or you can give the complexType element a name, and let the "person" element have a type attribute that refers to the name of the complexType (if you use this method, several elements can refer to the same complex type):
或者你可以给complexType元素取个名字,让"person"元素有个类型属性,这个类型属性的名字可以参考使用complexType元素的名字(如果你用这个方法,几个元素可以同时参考使用相同的复合类型)。
<xs:element name="person" type="persontype"/> <xs:complexType name="persontype"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType> |
A complex text-only element can contain text and attributes.
一个复合只含文本元素(Complex Text-Only Elements)可以含有文本和属性。
Complex Text-Only Elements
复合只含文本元素(Complex Text-Only Elements)
This type contains only simple content (text and attributes), therefore we add a simpleContent element around the content. When using simple content, you must define an extension OR a restriction within the simpleContent element, like this:
这种类型只含有简单内容(文本和属性),因此我们在内容周围添加一个simpleContent元素,当用到简单内容时,你必须在simpleContent元素里定义一个扩展或约束,就像这样:
<xs:element name="somename"> <xs:complexType> <xs:simpleContent> <xs:extension base="basetype"> .... .... </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> OR <xs:element name="somename"> <xs:complexType> <xs:simpleContent> <xs:restriction base="basetype"> .... .... </xs:restriction> </xs:simpleContent> </xs:complexType> </xs:element> |
Tip: Use the extension/restriction element to expand or to limit the base simple type for the element.
提示:用extension/restriction元素扩展或限制元素的基本简单类型(base simple type)。
Here is an example of an XML element, "shoesize", that contains text-only:
这儿是只含有文本的一个XML元素, "shoesize":
<shoesize country="france">35</shoesize> |
The following example declares a complexType, "shoesize". The content is defined as an integer value, and the "shoesize" element also contains an attribute named "country":
下面的例子声明了一个复合类型,"shoesize"元素。内容定义为整数值,"shoesize"元件含有名为"country"的属性。
<xs:element name="shoesize"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:integer"> <xs:attribute name="country" type="xs:string" /> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element> |
We could also give the complexType element a name, and let the "shoesize" element have a type attribute that refers to the name of the complexType (if you use this method, several elements can refer to the same complex type):
我们可以给complexType元件起个名字,让"shoesize"元件有种类属性,种类属性的名字就是complexType元件的名字。(如果你用这种方法,几个元素指的可以是相同的复合类型)
<xs:element name="shoesize" type="shoetype"/> <xs:complexType name="shoetype"> <xs:simpleContent> <xs:extension base="xs:integer"> <xs:attribute name="country" type="xs:string" /> </xs:extension> </xs:simpleContent> </xs:complexType> |
A mixed complex type element can contain attributes, elements, and text.
混合内容的复合类型元素(XSD Complex Types Element With Mixed Content )可以含有属性,元素,和文本。
Complex Types with Mixed Content
混合内容的复合类型
An XML element, "letter", that contains both text and other elements:
一个XML元素,"letter",既含有文本又含有其他元素:
<letter> Dear Mr.<name>John Smith</name>. Your order <orderid>1032</orderid> will be shipped on <shipdate>2001-07-13</shipdate>. </letter> |
The following schema declares the "letter" element:
下面的XML公式声明了"letter"元素:
<xs:element name="letter"> <xs:complexType mixed="true"> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="orderid" type="xs:positiveInteger"/> <xs:element name="shipdate" type="xs:date"/> </xs:sequence> </xs:complexType> </xs:element> |
Note: To enable character data to appear between the child-elements of "letter", the mixed attribute must be set to "true". The <xs:sequence> tag means that the elements defined (name, orderid and shipdate) must appear in that order inside a "letter" element.
注意:为了使字符数据能出现在"letter"子元件之间,mixed属性必须设置为"true"。<xs:sequence>标签指出了已定义的元素(name, orderid 和shipdate)在"letter"元素里必须以指定的顺序出现
We could also give the complexType element a name, and let the "letter" element have a type attribute that refers to the name of the complexType (if you use this method, several elements can refer to the same complex type):
我们可以给这个complexType元素一个名称,并且让"letter"元素有一个引用了complexType的名称的种类属性(如果你用了这个方法,几个元素可以同时使用相同的复合类型):
<xs:element name="letter" type="lettertype"/> <xs:complexType name="lettertype" mixed="true"> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="orderid" type="xs:positiveInteger"/> <xs:element name="shipdate" type="xs:date"/> </xs:sequence> </xs:complexType> |
We can control HOW elements are to be used in documents with indicators.
用指示器(Indicators)我们可以控制文件中元素的使用方法(HOW TO USE)。
Indicators
指示器(Indicators)
There are seven indicators:
有7种指示器(Indicators)
Order indicators:
顺序指示器(Indicators)
- All
全部 - Choice
选择 - Sequence
按顺序
Occurrence indicators:
出现次数指示器(Indicators):
- maxOccurs
最多出现次数 - minOccurs
最少出现次数
Group indicators:
组指示器(Indicators):
- Group name
组名 - attributeGroup name
属性组名称
Order Indicators
顺序指示器(Indicators)
Order indicators are used to define the order of the elements.
顺序指示器(Indicators)用于指定元素的顺序。
All Indicator
全部指示器(Indicators)
The <all> indicator specifies that the child elements can appear in any order, and that each child element must occur only once:
<all>指示器(Indicators)指明了子元件可以以任何次序出现,并且每个子元件只能出现一次:
<xs:element name="person"> <xs:complexType> <xs:all> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:all> </xs:complexType> </xs:element> |
Note: When using the <all> indicator you can set the <minOccurs> indicator to 0 or 1 and the <maxOccurs> indicator can only be set to 1 (the <minOccurs> and <maxOccurs> are described later).
注意:用<all>指示器(Indicators)时你可以把<minOccurs>指示器(Indicators)设为0或1,<maxOccurs>指示器(Indicators)只能设为1(<minOccurs> 和 <maxOccurs> 我们后头再说)
Choice Indicator
选择指示器(Indicators)
The <choice> indicator specifies that either one child element or another can occur:
<choice>指示器(Indicators)指明了随便的子元素都可以出现:
<xs:element name="person"> <xs:complexType> <xs:choice> <xs:element name="employee" type="employee"/> <xs:element name="member" type="member"/> </xs:choice> </xs:complexType> </xs:element> |
Sequence Indicator
有序指示器(Indicators)
The <sequence> indicator specifies that the child elements must appear in a specific order:
<sequence>指示器(Indicators)指定了子元素必须以一个指明的顺序出现:
<xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> |
Occurrence Indicators
出现次数指示器(Indicators)
Occurrence indicators are used to define how often an element can occur.
出现次数指示器(Indicators)用于定义一个元素可以出现的次数
Note: For all "Order" and "Group" indicators (any, all, choice, sequence, group name, and group reference) the default value for maxOccurs and minOccurs is 1.
注意:对所有的"Order"和"Group"指示器(Indicators)(any, all, choice, sequence,group name, 和 group reference)来说,maxOccurs 和 minOccurs的默认值都是1
maxOccurs Indicator
最多出现次数指示器(Indicators)
The <maxOccurs> indicator specifies the maximum number of times an element can occur:
最多出现次数指示器(Indicators)指明了一个元素可以出现的最多次数:
<xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="full_name" type="xs:string"/> <xs:element name="child_name" type="xs:string" maxOccurs="10"/> </xs:sequence> </xs:complexType> </xs:element> |
The example above indicates that the "child_name" element can occur a minimum of one time (the default value for minOccurs is 1) and a maximum of ten times in the "person" element.
上面的例子指明了"child_name"元素在"person"元素里最少出现1次(minOccurs的默认值为1),最多出现10次
minOccurs Indicator
最少出现次数指示器(Indicators)
The <minOccurs> indicator specifies the minimum number of times an element can occur:
最少出现次数指示器(Indicators)指明了一个元素要出现的最小次数:
<xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="full_name" type="xs:string"/> <xs:element name="child_name" type="xs:string" maxOccurs="10" minOccurs="0"/> </xs:sequence> </xs:complexType> </xs:element> |
The example above indicates that the "child_name" element can occur a minimum of zero times and a maximum of ten times in the "person" element.
上面的例子指明了"child_name"元素在"person"元素里最少出现0次(minOccurs的默认值为1),最多出现10次
Tip: To allow an element to appear an unlimited number of times, use the maxOccurs="unbounded" statement:
提示:为使元件可以重复出现无数次,可以设置maxOccurs="unbounded"的状态
A working example:
实际作用例子:
An XML file called "Myfamily.xml":
名为"Myfamily.xml"的XML文件:
<?xml version="1.0" encoding="ISO-8859-1"?> <persons xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="family.xsd"> <person> <full_name>Hege Refsnes</full_name> <child_name>Cecilie</child_name> </person> <person> <full_name>Tove Refsnes</full_name> <child_name>Hege</child_name> <child_name>Stale</child_name> <child_name>Jim</child_name> <child_name>Borge</child_name> </person> <person> <full_name>Stale Refsnes</full_name> </person> </persons> |
The XML file above contains a root element named "persons". Inside this root element we have defined three "person" elements. Each "person" element must contain a "full_name" element and it can contain up to five "child_name" elements.
上面XML文件含有一个名为"persons"的根元素,这个根元素里面里我们已经定义了3个"person"元素。每个"person"元素必须含有一个"full_name"元素,而且最多可有5个"child_name"元素。
Here is the schema file "family.xsd":
下面是名为"family.xsd"的schema文件:
<?xml version="1.0" encoding="ISO-8859-1"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xs:element name="persons"> <xs:complexType> <xs:sequence> <xs:element name="person" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="full_name" type="xs:string"/> <xs:element name="child_name" type="xs:string" minOccurs="0" maxOccurs="5"/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> |
Group Indicators
组指示器(Indicators)
Group indicators are used to define related sets of elements.
组指示器(Indicators)用于定义相关的元素组。
Element Groups
元素组
Element groups are defined with the group declaration, like this:
元素组要定义组声明,像这样:
<xs:group name="groupname"> ... </xs:group> |
You must define an all, choice, or sequence element inside the group declaration. The following example defines a group named "persongroup", that defines a group of elements that must occur in an exact sequence:
你必须在组声明里定义一个all, choice,或sequence元素。下面的例子定义了一个名为"persongroup"的组,这个定义了一组元素必须以一定顺序出现:
<xs:group name="persongroup"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> <xs:element name="birthday" type="xs:date"/> </xs:sequence> </xs:group> |
After you have defined a group, you can reference it in another definition, like this:
定义了一个组后,你可以在另一个组参考它,像这样:
<xs:group name="persongroup"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> <xs:element name="birthday" type="xs:date"/> </xs:sequence> </xs:group> <xs:element name="person" type="personinfo"/> <xs:complexType name="personinfo"> <xs:sequence> <xs:group ref="persongroup"/> <xs:element name="country" type="xs:string"/> </xs:sequence> </xs:complexType> |
Attribute Groups
属性组
Attribute groups are defined with the attributeGroup declaration, like this:
属性组定义有attributeGroup(属性组)声明,像这样:
<xs:attributeGroup name="groupname"> ... </xs:attributeGroup> |
The following example defines an attribute group named "personattrgroup":
下面的例子定义了叫做"personattrgroup"的一个属性组:
<xs:attributeGroup name="personattrgroup"> <xs:attribute name="firstname" type="xs:string"/> <xs:attribute name="lastname" type="xs:string"/> <xs:attribute name="birthday" type="xs:date"/> </xs:attributeGroup> |
After you have defined an attribute group, you can reference it in another definition, like this:
定义了一个属性组之后,你可以在别的定义里参考它,像这样:
<xs:attributeGroup name="personattrgroup"> <xs:attribute name="firstname" type="xs:string"/> <xs:attribute name="lastname" type="xs:string"/> <xs:attribute name="birthday" type="xs:date"/> </xs:attributeGroup> <xs:element name="person"> <xs:complexType> <xs:attributeGroup ref="personattrgroup"/> </xs:complexType> </xs:element> |
The <any> element enables us to extend the XML document with elements not specified by the schema!
<any>元素可以使我们在XML文档中添加没有被schema 定义过的新元素从而扩充XML文档。
The <any> Element
<any>元素
The <any> element enables us to extend the XML document with elements not specified by the schema.
<any>元素可以使我们在XML文档中添加没有被schema 定义过的新元素从而扩充XML文档。
The following example is a fragment from an XML schema called "family.xsd". It shows a declaration for the "person" element. By using the <any> element we can extend (after <lastname>) the content of "person" with any element:
下面的例子是名为"family.xsd"的一份XML schema片段。它展示了"person"元素的声明。用上<any>元素,我们可以在"person"元素的内容里扩充任意元素(在<lastname>的后面)
<xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> <xs:any minOccurs="0"/> </xs:sequence> </xs:complexType> </xs:element> |
Now we want to extend the "person" element with a "children" element. In this case we can do so, even if the author of the schema above never declared any "children" element.
现在我们想在"person"元素中添加"children"元素,即使这篇schema的作者从未声明过什么"children"元素,我们也可以做到。
Look at this schema file, called "children.xsd":
请看下面名为"children.xsd"的schema文件:
<?xml version="1.0" encoding="ISO-8859-1"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3schools.com" xmlns="http://www.w3schools.com" elementFormDefault="qualified"> <xs:element name="children"> <xs:complexType> <xs:sequence> <xs:element name="childname" type="xs:string" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> |
The XML file below (called "Myfamily.xml"), uses components from two different schemas; "family.xsd" and "children.xsd":
下面的XML文件(叫做"Myfamily.xml"),用上了来自"family.xsd" 和"children.xsd"两篇不同schema的组件
<?xml version="1.0" encoding="ISO-8859-1"?> <persons xmlns="http://www.microsoft.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:SchemaLocation="http://www.microsoft.com family.xsd http://www.w3schools.com children.xsd"> <person> <firstname>Hege</firstname> <lastname>Refsnes</lastname> <children> <childname>Cecilie</childname> </children> </person> <person> <firstname>Stale</firstname> <lastname>Refsnes</lastname> </person> </persons> |
The XML file above is valid because the schema "family.xsd" allows us to extend the "person" element with an optional element after the "lastname" element.
上述XML文件是有效的,因为"family.xsd" schema允许我们在"person"元素里的"lastname"元素后面扩充一个任意元素。
The <any> and <anyAttribute> elements are used to make EXTENSIBLE documents! They allow documents to contain additional elements that are not declared in the main XML schema.
<any> 和<anyAttribute>元素是用于制造可扩展文档的!它们允许文档含有没有在主要XML schema里声明过的其它新元素。
The <anyAttribute> element enables us to extend the XML document with attributes not specified by the schema!
<anyAttribute>元素可使我们在XML文档中添加未被schema指定过的属性
The <anyAttribute> Element
<anyAttribute>元素
The <anyAttribute> element enables us to extend the XML document with attributes not specified by the schema.
<anyAttribute>元素可使我们在XML文档中添加未被schema指定过的属性
The following example is a fragment from an XML schema called "family.xsd". It shows a declaration for the "person" element. By using the <anyAttribute> element we can add any number of attributes to the "person" element:
下面是名为"family.xsd"的XML schema片段。它显示了"person"元素的声明。通过使用<anyAttribute>元素我们可以给"person"元素添加任意数量的属性。
<xs:element name="person"> |
Now we want to extend the "person" element with a "gender" attribute. In this case we can do so, even if the author of the schema above never declared any "gender" attribute.
现在我们想在"person"元素中添加"gender"属性,即使这篇schema的作者从未声明过什么"gender"属性,我们也可以做到。
Look at this schema file, called "attribute.xsd":
请看下面名为"attribute.xsd"的schema文件:
<?xml version="1.0" encoding="ISO-8859-1"?> <xs:attribute name="gender"> </xs:schema> |
The XML file below (called "Myfamily.xml"), uses components from two different schemas; "family.xsd" and "attribute.xsd":
下面的XML文件(叫做"Myfamily.xml"),用上了来自"family.xsd" 和"attribute.xsd"两篇不同的schema组件。
<?xml version="1.0" encoding="ISO-8859-1"?> <persons xmlns="http://www.microsoft.com" <person gender="female"> <person gender="male"> </persons> |
The XML file above is valid because the schema "family.xsd" allows us to add an attribute to the "person" element.
上述XML文件是有效的,因为"family.xsd" schema允许我们在"person"元素里添加属性
The <any> and <anyAttribute> elements are used to make EXTENSIBLE documents! They allow documents to contain additional elements that are not declared in the main XML schema.
<any> 和<anyAttribute>元素是用于扩展文档的!它们允许文档含有没有在主要XML schema里声明过的其它新元素。
With XML Schemas, one element can substitute another element.
用XML Schema,一个元素可替代另一元素。
Element Substitution
元素替代(Element Substitution)
Let's say that we have users from two different countries: England and Norway. We would like the ability to let the user choose whether he or she would like to use the Norwegian element names or the English element names in the XML document.
假设有两个分别来自英国和挪威的使用者,我们很希望有能力能让他或她进行选择,在XML文档里的元素名称中选择他们所擅长的语言,是英文呢?还是挪威文呢?
To solve this problem, we could define a substitutionGroup in the XML schema. First, we declare a head element and then we declare the other elements which state that they are substitutable for the head element.
为解决这个问题,我们在XML schema里定义了替代组。首先,我们声明了一个标题元素,接着,我们声明已说明可替代标题元素的其他元素
<xs:element name="name" type="xs:string"/> |
In the example above, the "name" element is the head element and the "navn" element is substitutable for "name".
在上面的例子里,"name"元素是标题元素,"navn"元素可以替代"name"。
Look at this fragment of an XML schema:
看下面的XML schema片段:
<xs:element name="name" type="xs:string"/> <xs:complexType name="custinfo"> <xs:element name="customer" type="custinfo"/> |
A valid XML document (according to the schema above) could look like this:
一份有效 的XML文档(根据上述schema的XML文档)应该像这样:
<customer> |
or like this:
或者像这样:
<kunde> |
Blocking Element Substitution
关闭元素替代(Element Substitution)
To prevent other elements from substituting with a specified element, use the block attribute:
为了防止其他元素被已指定的元素替代(Element Substitution),可以用block属性:
<xs:element name="name" type="xs:string" block="substitution"/> |
Look at this fragment of an XML schema:
看这段XML schema片段:
<xs:element name="name" type="xs:string" block="substitution"/> <xs:complexType name="custinfo"> <xs:element name="customer" type="custinfo" block="substitution"/> |
A valid XML document (according to the schema above) looks like this:
一份有效的XML文档(根据上述的schema的XML文档)应该像这样:
<customer> |
BUT THIS IS NO LONGER VALID:
但是这样就不再有效了
<kunde> |
Using substitutionGroup
使用替代组(substitutionGroup)
The type of the substitutable elements must be the same as, or derived from, the type of the head element. If the type of the substitutable element is the same as the type of the head element you will not have to specify the type of the substitutable element.
可替代元素类型应和标题元素的类型相同,或是从中派生出来的。如果可替代元素类型和标题元素的类型相同,你就不需要再指明可替代元素的类型了
Note that all elements in the substitutionGroup (the head element and the substitutable elements) must be declared as global elements, otherwise it will not work!
注意在可替代元素组里的所有元素(标题元素和可替代元素)必须声明为“全域元素(global element)”,否则它是不会作用的!
What are Global Elements?
什么是“全域元素”?
Global elements are elements that are immediate children of the "schema" element! Local elements are elements nested within other elements.
“全域元素”是"schema"元素下面的直接子元素。“本地元素”是嵌套在别的元素里的元素。
This chapter will demonstrate how to write an XML Schema. You will also learn that a schema can be written in different ways.
这章将示范如何写一份XML Schema.你也会了解到可以使用不同的方法书写schema。
An XML Document
一份XML文档
Let's have a look at this XML document called "shiporder.xml":
让我们看这份名为"shiporder.xml"的XML文档
<?xml version="1.0" encoding="ISO-8859-1"?> <shiporder orderid="889923" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="shiporder.xsd"> <orderperson>John Smith</orderperson> <shipto> <name>Ola Nordmann</name> <address>Langgt 23</address> <city>4000 Stavanger</city> <country>Norway</country> </shipto> <item> <title>Empire Burlesque</title> <note>Special Edition</note> <quantity>1</quantity> <price>10.90</price> </item> <item> <title>Hide your heart</title> <quantity>1</quantity> <price>9.90</price> </item> </shiporder> |
The XML document above consists of a root element, "shiporder", that contains a required attribute called "orderid". The "shiporder" element contains three different child elements: "orderperson", "shipto" and "item". The "item" element appears twice, and it contains a "title", an optional "note" element, a "quantity", and a "price" element.
上述XML文档有根目录元素"shiporder",它有个必需的属性叫做"orderid","shiporder"元素包含了三个不同的子元素:"orderperson", "shipto" 和 "item"."item"元素出现了两次,它包含了一个"title"元素,一个任意的"note"元素,一个"quantity"元素和一个"price"元素。
The line above: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" tells the XML parser that this document should be validated against a schema. The line:
xsi:noNamespaceSchemaLocation="shiporder.xsd" specifies WHERE the schema resides (here it is in the same folder as "shiporder.xml").
上面的一行xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance",告诉了XML解析器这个文档应该被一份schema检验。xsi:noNamespaceSchemaLocation="shiporder.xsd"指定了schema应该所在位置(这里它以"shiporder.xml"文件待在相同的文件夹里)。
Create an XML Schema
创建一篇XML Schema
Now we want to create a schema for the XML document above.
现在我们想要为上面的XML文档创建一份schema
We start by opening a new file that we will call "shiporder.xsd". To create the schema we could simply follow the structure in the XML document and define each element as we find it. We will start with the standard XML declaration followed by the xs:schema element that defines a schema:
开始先打开叫做"shiporder.xsd"的新文件。为创建新的schema,我们可以简单地按照XML文档的结构,每当发现一个元素时就进行定义。一开始先作标准的XML声明,接下来是定义了schema的xs:schema元素:
<?xml version="1.0" encoding="ISO-8859-1" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> ... ... </xs:schema> |
In the schema above we use the standard namespace (xs), and the URI associated with this namespace is the Schema language definition, which has the standard value of http://www.w3.org/2001/XMLSchema.
在上述schema里我们用了标准的名称空间(xs),并且和这个名称空间相联系的URI是Schema语言定义,它有个标准值http://www.w3.org/2001/XMLSchema.
Next, we have to define the "shiporder" element. This element has an attribute and it contains other elements, therefore we consider it as a complex type. The child elements of the "shiporder" element is surrounded by a xs:sequence element that defines an ordered sequence of sub elements:
接下来,我们必须定义"shiporder”元素。这个元素有个属性,并包含着其他的元素,因此我们把它看成是复合类型。"shiporder"元素的子元素由xs:sequence元素包围着,xs:sequence元素定义了子元素一定顺序的排列。
<xs:element name="shiporder"> <xs:complexType> <xs:sequence> ... ... </xs:sequence> ... </xs:complexType> </xs:element> |
Then we have to define the "orderperson" element as a simple type (because it does not contain any attributes or other elements). The type (xs:string) is prefixed with the namespace prefix associated with XML Schema that indicates a predefined schema data type:
那么我们必须将"orderperson"元件定义为简单类型(因为它不含有任何属性或其它元素)。种类用名称空间(namespace)的前缀使用与XML Schema相关的名称空间前缀化,这里的XML Schema指明了一个前缀的Schema数据类型。
<xs:element name="orderperson" type="xs:string"/> |
Next, we have to define two elements that are of the complex type: "shipto" and "item". We start by defining the "shipto" element:
接下来,我们必须定义两个复合类型的元素,"shipto" 和 "item"。先由定义"shipto"元素开始:
<xs:element name="shipto"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="address" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="country" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> |
With schemas we can define the number of possible occurrences for an element with the maxOccurs and minOccurs attributes. maxOccurs specifies the maximum number of occurrences for an element and minOccurs specifies the minimum number of occurrences for an element. The default value for both maxOccurs and minOccurs is 1!
用schema,我们可以用maxOccurs(最多出现次数)和minOccurs(最少出现次数)属性来定义一个元素的可能出现次数。maxOccurs指定了一个元素的最多出现次数,minOccurs指定了一个元素的最少出现次数。maxOccurs
和minOccurs的默认值都是1。
Now we can define the "item" element. This element can appear multiple times inside a "shiporder" element. This is specified by setting the maxOccurs attribute of the "item" element to "unbounded" which means that there can be as many occurrences of the "item" element as the author wishes. Notice that the "note" element is optional. We have specified this by setting the minOccurs attribute to zero:
现在我们可以来定义"item"元素,这个元素可以在"shiporder"元素里重复出现。这可以通过设置"item"元素的maxOccurs属性为"unbounded"实现,属性为"unbounded"意味着"item"元素可以根据编者意愿重复出现多次。要注意"note"元素是任意的,我们可以通过设置minOccurs属性为0来实现。
<xs:element name="item" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element name="note" type="xs:string" minOccurs="0"/> <xs:element name="quantity" type="xs:positiveInteger"/> <xs:element name="price" type="xs:decimal"/> </xs:sequence> </xs:complexType> </xs:element> |
We can now declare the attribute of the "shiporder" element. Since this is a required attribute we specify use="required".
我们现在可以声明"shiporder"元素的属性了。因为这是项必须属性,我们可以指定:use="required"。
Note: The attribute declarations must always come last:
注意:属性声明必须总是放在最后
<xs:attribute name="orderid" type="xs:string" use="required"/> |
Here is the complete listing of the schema file called "shiporder.xsd":
下面是"shiporder.xsd" schema文件的完整例子:
<?xml version="1.0" encoding="ISO-8859-1" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="shiporder"> <xs:complexType> <xs:sequence> <xs:element name="orderperson" type="xs:string"/> <xs:element name="shipto"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="address" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="country" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="item" maxOccurs="unbounded"> <xs:complexType> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element name="note" type="xs:string" minOccurs="0"/> <xs:element name="quantity" type="xs:positiveInteger"/> <xs:element name="price" type="xs:decimal"/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> <xs:attribute name="orderid" type="xs:string" use="required"/> </xs:complexType> </xs:element> </xs:schema> |
Divide the Schema
划分Schema
The previous design method is very simple, but can be difficult to read and maintain when documents are complex.
上面的设计方法非常简单,但因为文件是复合(或复杂的,Complex)的,所以难于阅读和利用。
The next design method is based on defining all elements and attributes first, and then referring to them using the ref attribute.
下面的设计方法是:先定义所有的元素和属性,然后用ref属性引用它们。
Here is the new design of the schema file ("shiporder.xsd"):
下面是schema文件的新设计方式("shiporder.xsd"):
<?xml version="1.0" encoding="ISO-8859-1" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <!-- definition of simple elements --> <xs:element name="orderperson" type="xs:string"/> <xs:element name="name" type="xs:string"/> <xs:element name="address" type="xs:string"/> <xs:element name="city" type="xs:string"/> <xs:element name="country" type="xs:string"/> <xs:element name="title" type="xs:string"/> <xs:element name="note" type="xs:string"/> <xs:element name="quantity" type="xs:positiveInteger"/> <xs:element name="price" type="xs:decimal"/> <!-- definition of attributes --> <xs:attribute name="orderid" type="xs:string"/> <!-- definition of complex elements --> <xs:element name="shipto"> <xs:complexType> <xs:sequence> <xs:element ref="name"/> <xs:element ref="address"/> <xs:element ref="city"/> <xs:element ref="country"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="item"> <xs:complexType> <xs:sequence> <xs:element ref="title"/> <xs:element ref="note" minOccurs="0"/> <xs:element ref="quantity"/> <xs:element ref="price"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="shiporder"> <xs:complexType> <xs:sequence> <xs:element ref="orderperson"/> <xs:element ref="shipto"/> <xs:element ref="item" maxOccurs="unbounded"/> </xs:sequence> <xs:attribute ref="orderid" use="required"/> </xs:complexType> </xs:element> </xs:schema> |
Using Named Types
使用有名称的类型
The third design method defines classes or types, that enables us to reuse element definitions. This is done by naming the simpleTypes and complexTypes elements, and then point to them through the type attribute of the element.
第三种设计方法定义了种类或类型,这使我们能重新用元素定义。通过给简单类型和复合类型元素命名,接着在元素的种类属性类型里指明它们的方法来做到这点。
Here is the third design of the schema file ("shiporder.xsd"):
这是schema文件("shiporder.xsd")的第三份构思
<?xml version="1.0" encoding="ISO-8859-1" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:simpleType name="stringtype"> <xs:restriction base="xs:string"/> </xs:simpleType> <xs:simpleType name="inttype"> <xs:restriction base="xs:positiveInteger"/> </xs:simpleType> <xs:simpleType name="dectype"> <xs:restriction base="xs:decimal"/> </xs:simpleType> <xs:simpleType name="orderidtype"> <xs:restriction base="xs:string"> <xs:pattern value="[0-9]{6}"/> </xs:restriction> </xs:simpleType> <xs:complexType name="shiptotype"> <xs:sequence> <xs:element name="name" type="stringtype"/> <xs:element name="address" type="stringtype"/> <xs:element name="city" type="stringtype"/> <xs:element name="country" type="stringtype"/> </xs:sequence> </xs:complexType> <xs:complexType name="itemtype"> <xs:sequence> <xs:element name="title" type="stringtype"/> <xs:element name="note" type="stringtype" minOccurs="0"/> <xs:element name="quantity" type="inttype"/> <xs:element name="price" type="dectype"/> </xs:sequence> </xs:complexType> <xs:complexType name="shipordertype"> <xs:sequence> <xs:element name="orderperson" type="stringtype"/> <xs:element name="shipto" type="shiptotype"/> <xs:element name="item" maxOccurs="unbounded" type="itemtype"/> </xs:sequence> <xs:attribute name="orderid" type="orderidtype" use="required"/> </xs:complexType> <xs:element name="shiporder" type="shipordertype"/> </xs:schema> |
The restriction element indicates that the datatype is derived from a W3C XML Schema namespace datatype. So, the following fragment means that the value of the element or attribute must be a string value:
约束元素指出了这个数据类型是由一个W3C XML Schema名称空间数据类型派生出来的。所以,下面的片段意味着元素或属性的值必须是一个字符串的值
<xs:restriction base="xs:string"> |
The restriction element is more often used to apply restrictions to elements. Look at the following lines from the schema above:
约束元素常常用于给元素添加约束。请看上述schema中的几行:
<xs:simpleType name="orderidtype"> <xs:restriction base="xs:string"> <xs:pattern value="[0-9]{6}"/> </xs:restriction> </xs:simpleType> |
This indicates that the value of the element or attribute must be a string, it must be exactly six characters in a row, and those characters must be a number from 0 to 9.
这指出了元素或属性的值必须是字符串,而且必须是一排6个从0到9之间的数字。
String data types are used for values that contains character strings.
字符串数据类型用于定义字符串的值的。
String Data Type
字符串数据类型
The string data type can contain characters, line feeds, carriage returns, and tab characters.
字符串数据类型包括字符,换行符,回车符,和制表符
The following is an example of a string declaration in a schema:
下面是关于字符在schema(XML公式)里的声明方法的例子:
<xs:element name="customer" type="xs:string"/> |
An element in your document might look like this:
你文档中也许有这样的元素:
<customer>John Smith</customer> |
Or it might look like this:
它也许会是这样:
<customer> John Smith </customer> |
Note: The XML processor will not modify the value if you use the string data type.
注意:如果你用字符串数据类型,XML处理器将不会修改数值。
NormalizedString Data Type
规格化的字符串数据类型(NormalizedString Data Type)
The normalizedString data type is derived from the String data type.
规格化的字符串数据类型是从字符数据类型里派生出来的。
The normalizedString data type also contains characters, but the XML processor will remove line feeds, carriage returns, and tab characters.
规格化的字符串数据类型也包括字符,但XML processor会清除换行符,回车符和制表符
The following is an example of a normalizedString declaration in a schema:
下面的是关于一个规格化的字符串数据类型(normalizedString)在Schema中声明方法的例子:
<xs:element name="customer" type="xs:normalizedString"/> |
An element in your document might look like this:
你的文档中也许有像这样的元素:
<customer>John Smith</customer> |
Or it might look like this:
或者它也许会是这样:
<customer> John Smith </customer> |
Note: In the example above the XML processor will replace the tabs with spaces.
注意:上述例子里XML处理器会用空格符替代制表符
Token Data Type
记号(token)数据类型
The token data type is also derived from the String data type.
记号数据类型也是从字符串数据类型里派生出来的
The token data type also contains characters, but the XML processor will remove line feeds, carriage returns, tabs, leading and trailing spaces, and multiple spaces.
记号数据类型也包括字符,但XML处理器会清除换行符,回车符,制表符,头尾的空格,以及对于的空格。
The following is an example of a token declaration in a schema:
下面是关于符号(token)在schema中声明的例子:
<xs:element name="customer" type="xs:token"/> |
An element in your document might look like this:
你文档中的元素也许像这样:
<customer>John Smith</customer> |
Or it might look like this:
或者它也有可能像这样:
<customer> John Smith </customer> |
Note: In the example above the XML processor will remove the tabs.
注意:在上述例子里,XML处理器会清除制表符
String Data Types
字符串数据类型
Note that all of the data types below derive from the String data type (except for string itself)!
要注意下面所有的数据类型都是从字符串数据类型里派生出来的(除了字符串本身)
Name 名称 |
Description 解释 |
---|---|
ENTITIES | |
ENTITY | |
ID | A string that represents the ID attribute in XML (only used with schema attributes) 象征XML的ID属性的字符串(只用在schema属性里) |
IDREF | A string that represents the IDREF attribute in XML (only used with schema attributes) 象征XML里的IDREF属性的字符串(只能和schema属性一起使用) |
IDREFS | |
language | A string that contains a valid language id 含有正确的语言ID的字符串 |
Name | A string that contains a valid XML name 含有一个有效的XML名称的字符串 |
NCName | |
NMTOKEN | A string that represents the NMTOKEN attribute in XML (only used with schema attributes) 象征XML里的NMTOKEN属性的字符串(只能和schema属性一起使用) |
NMTOKENS | |
normalizedString | A string that does not contain line feeds, carriage returns, or tabs 不含有换行符,回车符或制表符的字符串 |
QName | |
string | A string 一个字符串 |
token | A string that does not contain line feeds, carriage returns, tabs, leading or trailing spaces, or multiple spaces 不含有换行符,回车符,制表符,头尾空格,或重复空格的字符串 |
Restrictions on String Data Types
对字符串数据类型的约束(Restrictions on String Data Types)
Restrictions that can be used with String data types:
可约束的字符串数据类型:
- enumeration
列举 - length
长度 - maxLength
最大长度 - minLength
最小长度 - pattern (NMTOKENS, IDREFS, and ENTITIES cannot use this constraint)
式样(不能约束NMTOKENS, IDREFS,和 ENTITIES) - whiteSpace
空白
Date and time data types are used for values that contain date and time.
日期和时间数据类型(Date and time data types)用于定义日期和时间数据的。
Date Data Type
日期数据类型The date data type is used to specify a date.
日期数据类型用于指定一个具体的日期The date is specified in the following form "YYYY-MM-DD" where:
日期由下面的"YYYY-MM-DD"形式指定。- YYYY indicates the year
YYYY指示年份 - MM indicates the month
MM指示月份 - DD indicates the day
DD 指示日期
Note: All components are required!
注意:所有组件都是必需的。The following is an example of a date declaration in a schema:
下面是schema(XML公式)里的声明日期的例子<xs:element name="start" type="xs:date"/>
An element in your document might look like this:
你文档中的某个元素也许是这样的:<start>2002-09-24</start>
Time Zones
时间区To specify a time zone, you can either enter a date in UTC time by adding a "Z" behind the date - like this:
为指定一个时间区,你既可以通过在日期的后面加个"Z"访问UTC时间格式的日期,就像这样:<start>2002-09-24Z</start>
or you can specify an offset from the UTC time by adding a positive or negative time behind the date - like this:
也可以UTC时间后加、减一个时间值,就像这样:<start>2002-09-24-06:00</start>
or
<start>2002-09-24+06:00</start>
Time Data Type
时间数据类型The time data type is used to specify a time.
时间数据类型用于指定一个时间The time is specified in the following form "hh:mm:ss" where:
时间由下面的"hh:mm:ss"形式指定:- hh indicates the hour
hh指示小时 - mm indicates the minute
mm 指示分钟 - ss indicates the second
ss指示秒钟
Note: All components are required!
注意:所有的组件都是必需的The following is an example of a time declaration in a schema:
下面是schema(XML公式)里的时间声明的例子<xs:element name="start" type="xs:time"/>
An element in your document might look like this:
你文档中或许有这样的元素:<start>09:00:00</start>
Or it might look like this:
或者是这样的:<start>09:30:10.5</start>
Time Zones
时间区To specify a time zone, you can either enter a time in UTC time by adding a "Z" behind the time - like this:
为指定一个时间区,你既可以通过在日期的后面加个"Z"访问UTC时间格式的日期,就像这样:<start>09:30:10Z</start>
or you can specify an offset from the UTC time by adding a positive or negative time behind the time - like this:
也可以UTC时间后加、减一个时间值,就像这样:<start>09:30:10-06:00</start>
or
<start>09:30:10+06:00</start>
DateTime Data Type
日期时间数据类型(DateTime Data Type)The dateTime data type is used to specify a date and a time.
日期时间数据类型用于指定一个日期和一个时间The dateTime is specified in the following form "YYYY-MM-DDThh:mm:ss" where:
日期时间由"YYYY-MM-DDThh:mm:ss"的形式指定:- YYYY indicates the year
YYYY指示时间 - MM indicates the month
MM指示月份 - DD indicates the day
DD 指示时间 - T indicates the start of the required time section
T指示时间区的开头 - hh indicates the hour
hh指示小时 - mm indicates the minute
mm指示分钟 - ss indicates the second
ss指示秒钟
Note: All components are required!
注意:所有组件都是必需的The following is an example of a dateTime declaration in a schema:
下面是schema(XML公式)里的日期时间声明的例子<xs:element name="startdate" type="xs:dateTime"/>
An element in your document might look like this:
你文档中或许有这样的元素:<startdate>2002-05-30T09:00:00</startdate>
Or it might look like this:
或者是这样的:<startdate>2002-05-30T09:30:10.5</startdate>
Time Zones
时间区To specify a time zone, you can either enter a dateTime in UTC time by adding a "Z" behind the time - like this:
为指定一个时间区,你既可以通过在日期的后面加个"Z"访问UTC时间格式的日期,就像这样:<startdate>2002-05-30T09:30:10Z</startdate>
or you can specify an offset from the UTC time by adding a positive or negative time behind the time - like this:
也可以UTC时间后加、减一个时间值,就像这样:<startdate>2002-05-30T09:30:10-06:00</startdate>
or
<startdate>2002-05-30T09:30:10+06:00</startdate>
Duration Data Type
持续时间数据类型(或“时间段数据类型”)(Duration Data Type)The duration data type is used to specify a time interval.
持续时间数据类型(或“时间段数据类型”)用于定义一段时间间隔The time interval is specified in the following form "PnYnMnDTnHnMnS" where:
时间间隔由"PnYnMnDTnHnMnS"形式定义的- P indicates the period (required)
P指示 - nY indicates the number of years
nY指示年数 - nM indicates the number of months
nM指示月数 - nD indicates the number of days
nD指示天数 - T indicates the start of a time section (required if you are going to specify hours, minutes, or seconds)
T指示时间部分的开头(如果你要指定时间,分钟,或秒数,就必须用到T) - nH indicates the number of hours
nH指示小时数 - nM indicates the number of minutes
nM指示分钟数 - nS indicates the number of seconds
nS指示秒数
The following is an example of a duration declaration in a schema:
下面是schema(XML公式)里的时间段声明的例子<xs:element name="period" type="xs:duration"/>
An element in your document might look like this:
你文档中或许有这样的元素:<period>P5Y</period>
The example above indicates a period of five years.
上面的例子指示了5年的时间段Or it might look like this:
或者也可以像这样:<period>P5Y2M10D</period>
The example above indicates a period of five years, two months, and 10 days.
上面的例子指出了5年2个月零10天的时间段Or it might look like this:
它也可以这样:<period>P5Y2M10DT15H</period>
The example above indicates a period of five years, two months, 10 days, and 15 hours.
上面的例子指出了5年2个月零10天的时间段Or it might look like this:
或者它会是可以这样:<period>PT15H</period>
The example above indicates a period of 15 hours.
上面的例子指出了15小时的时间段Negative Duration
负时间段To specify a negative duration, enter a minus sign before the P:
为定义负的时间段,在P之前添个负号<period>-P10D</period>
The example above indicates a period of minus 10 days.
上面的例子指出里负10天的时间段
Date and Time Data Types
日期和时间数据类型Name
名称Description
解释date Defines a date value
定义日期的值dateTime Defines a date and time value
定义了日期和时间值duration Defines a time interval
定义了时间间隔gDay Defines a part of a date - the day (DD)
定义了日期的部分定义了时间的一部分——日部分(DD)gMonth Defines a part of a date - the month (MM)
定义了日期的一部分——月部分(MM)gMonthDay Defines a part of a date - the month and day (MM-DD)
定义了日期的一部分——月和日部分(MM-DD)gYear Defines a part of a date - the year (YYYY)
定义了日期的年部分(YYYY)gYearMonth Defines a part of a date - the year and month (YYYY-MM)
定义了日期的一部分——年和月部分(YYYY-MM)time Defines a time value
定义时间值
Restrictions on Date Data Types
对日期数据类型的约束Restrictions that can be used with Date data types:
对日期数据类型可进行约束- enumeration
列举 - maxExclusive
除去最大值 - maxInclusive
包括最大值 - minExclusive
除去最小值排外 - minInclusive
包括最小值 - pattern
式样 - whiteSpace
空白
- YYYY indicates the year
Decimal data types are used for numeric values.
小数数据类型用于定义数值
Decimal Data Type
小数数据类型
The decimal data type is used to specify a numeric value.
小数数据类型用于指定一个数值
The following is an example of a decimal declaration in a schema:
下面是在schema(XML公式)里关于小数声明的例子
<xs:element name="prize" type="xs:decimal"/> |
An element in your document might look like this:
你文档中的一个元素也许会像这样:
<prize>999.50</prize> |
Or it might look like this:
或者会像这样:
<prize>+999.5450</prize> |
Or it might look like this:
又或者像这样:
<prize>-999.5230</prize> |
Or it might look like this:
也可能这样:
<prize>0</prize> |
Or it might look like this:
当然,也会是这样:
<prize>14</prize> |
Note: The maximum number of decimal digits you can specify is 18.
注意:可以指定的小数位数最多是18位。
Integer Data Type
整数数据类型
The integer data type is used to specify a numeric value without a fractional component.
整数数据类型用于指定没有小数部分的数值。
The following is an example of an integer declaration in a schema:
下面是的关于整数在schema里的声明方法的例子。
<xs:element name="prize" type="xs:integer"/> |
An element in your document might look like this:
你文档中也许有这样的元素:
<prize>999</prize> |
Or it might look like this:
或许是这样:
<prize>+999</prize> |
Or it might look like this:
也可能是这样:
<prize>-999</prize> |
Or it might look like this:
当然也会是这样:
<prize>0</prize> |
Numeric Data Types
数字数据类型
Note that all of the data types below derive from the Decimal data type (except for decimal itself)!
要注意的是下面所有的数据类型都是从小数数据类型里派生出来的(除了小数自身)
Name 名称 |
Description 解释 |
---|---|
byte | A signed 8-bit integer 最大8位的带符号整数 |
decimal | A decimal value 小数数值 |
int | A signed 32-bit integer 最大32位的带符号整数 |
integer | An integer value 整数值 |
long | A signed 64-bit integer 最大64位的带符号整数 |
negativeInteger | An integer containing only negative values ( .., -2, -1.) 含有负值的整数 ( .., -2, -1.) |
nonNegativeInteger | An integer containing only non-negative values (0, 1, 2, ..) 含有非负值的整数 (0, 1, 2, ..) |
nonPositiveInteger | An integer containing only non-positive values (.., -2, -1, 0) 含有非正值的整数 (.., -2, -1, 0) |
positiveInteger | An integer containing only positive values (1, 2, ..) 只含有正值的整数(1, 2, ..) |
short | A signed 16-bit integer 最大16位的带符号整数 |
unsignedLong | An unsigned 64-bit integer 最大64位的不带符号整数 |
unsignedInt | An unsigned 32-bit integer 最大32位的不带符号整数 |
unsignedShort | An unsigned 16-bit integer 最大16位的不带符号整数 |
unsignedByte | An unsigned 8-bit integer 最大8位的不带符号整数 |
Restrictions on Numeric Data Types
对字数据类型的约束
Restrictions that can be used with Numeric data types:
可用于约束的数字数据类型:
- enumeration
列举 - fractionDigits
小数位数 - maxExclusive
除去最大值 - maxInclusive
包括最大值 - minExclusive
除去最小值 - minInclusive
包括最小值 - pattern
式样类型 - totalDigits
总的数字位数 - whiteSpace
空白部分
Other miscellaneous data types are boolean, base64Binary, hexBinary, float, double, anyURI, QName, and NOTATION.
其他混合的数据类型包括逻辑值(boolean),基本的64个二进制数、十六进位数、浮点精度小数、双精度型数、anyURI、限定名称(QName)和符号。
Boolean Data Type
逻辑值数据类型The boolean data type is used to specify a true or false value.
逻辑型数据类型用于指定值为真或假。The following is an example of a boolean declaration in a schema:
以下是在schema(XML公式)里有关逻辑声明的例子<xs:attribute name="disabled" type="xs:boolean"/>
An element in your document might look like this:
你文档里或许有这样的元素:<prize disabled="true">999</prize>
Note: Legal values for boolean are true, false, 1 (which indicates true), and 0 (which indicates false).
注意:合法的逻辑值是真,假,1(指示真),0(指示假)
Binary Data Types
二进制数据类型Binary data types are used to express binary-formatted data.
二进制数据用于表示二进制的数据We have two binary data types:
我们有两种二进制数据类型:- base64Binary (Base64-encoded binary data)
基础64二进制(64码基础的二进制数据Base64-encoded binary data) - hexBinary (hexadecimal-encoded binary data)
16进制(16进位编码的二进制数据hexadecimal-encoded binary data)
The following is an example of a hexBinary declaration in a schema:
以下是关于十六进位数在schema里的声明方法:<xs:element name="blobsrc" type="xs:hexBinary"/>
AnyURI Data Type
anyURI数据类型The anyURI data type is used to specify a URI.
anyURI数据类型用于指定一个URI.The following is an example of an anyURI declaration in a schema:
以下是关于anyURI在schema里的声明方法:<xs:attribute name="src" type="xs:anyURI"/>
An element in your document might look like this:
你文档里也许有这个元素:<pic src="http://www.w3schools.com/images/smiley.gif" />
Note: If a URI has spaces, replace them with %20.
注意:如果URI包含空格,可以用“%20”替代它们。
Miscellaneous Data Types
混合数据类型Name
名称Description
描述anyURI base64Binary boolean double float hexBinary NOTATION QName
Restrictions on Miscellaneous Data Types
对混合数据类型的约束Restrictions that can be used with the other data types:
可用于约束的其他数据类型:- enumeration (a Boolean data type cannot use this constraint)
列举(不能用此约束逻辑值数据类型) - length (a Boolean data type cannot use this constraint)
长度(不能用此约束逻辑值数据类型) - maxLength (a Boolean data type cannot use this constraint)
最大长度(不能用此约束逻辑值数据类型) - minLength (a Boolean data type cannot use this constraint)
最短长度(不能用此约束逻辑值数据类型) - pattern
式样 - whiteSpace
空白
- base64Binary (Base64-encoded binary data)
XML Schema Summary
XML Schema总结
This tutorial has taught you how to describe the structure of an XML document.
这篇教程教你如何描述XML文档的结构
You have learned how to use an XML Schema is to define the legal elements of an XML document, just like a DTD. We think that very soon XML Schemas will be used in most Web applications as a replacement for DTDs.
你已经学习了怎样使用XML Schema(XML 公式),就是象DTD一样给XML文档定义合法元素。我们认为很快XML Schema(XML 公式)就会取代DTD而被大多数的网络应用程序所使用。
You have also learned that the XML Schema language is very rich. Unlike DTDs, it supports data types and namespaces.
你已经知道了 XML公式语言是很丰富的。和DTD不同,它支持数据类型和名称空间。
For more information on XML Schema, please look at our XML Schema reference.
想更多地了解XML Schema 的信息,请看我们的XML Schema 参考。
Now You Know XML Schema, What's Next?
现在你知道了XML Schema,那接着学什么呢?
The next step is to learn about WSDL.
下步是学习有关WSDL.
WSDL is a schema-based language for describing Web services and how to access them.
WSDL 是描述网络服务以及描述怎样访问它们的一种以schema为基础的语言
WSDL describes a web service, along with the message format and protocol details for the web service.
WSDL描述网络服务,以及与网络服务有关的信息格式和协议细节。
If you want to learn more about WSDL, please visit our WSDL tutorial.
想学习更多与WSDL有关的内容,请浏览我们的WSDL 教程.
XSD Elements
XSD元素
Element 元素 |
Explanation 解释 |
---|---|
all | Specifies that the child elements can appear in any order. Each child element can occur 0 or 1 time 指明了子元素可以以任意顺序出现,每个子元素出现0到1次 |
annotation | Specifies the top-level element for schema comments 为schema注释指定了最高阶层的元素 |
any | Enables the author to extend the XML document with elements not specified by the schema 让编者能够用没有被schema指定的元素来扩展XML文件 |
anyAttribute | Enables the author to extend the XML document with attributes not specified by the schema 让编者能够用没有被schema指定的属性来扩展XML文件 |
appInfo | Specifies information to be used by the application (must go inside annotation) 指定了将被应用程序所用到的信息(必须在annotation里) |
attribute | Defines an attribute 定义属性 |
attributeGroup | Defines an attribute group to be used in complex type definitions 定义了用于复合种类定义的属性组 |
choice | Allows only one of the elements contained in the <choice> declaration to be present within the containing element 只允许<choice>声明里的元素里的一个元素出现在所含的元素中。 |
complexContent | Defines extensions or restrictions on a complex type that contains mixed content or elements only 在包含混合内容和元素的复合类型(complex type)上定义扩展或限制 |
complexType | Defines a complex type element 定义了复合类型元素(complex type element) |
documentation | Defines text comments in a schema (must go inside annotation) 定义了schema里的文本注释(必须在annotation里) |
element | Defines an element 定义元素 |
extension | Extends an existing simpleType or complexType element 扩展一个现存的简单类型或复合类型元素 |
field | Specifies an XPath expression that specifies the value used to define an identity constraint 指明一个XPath表达式,以指明用于定义身份约束的值 |
group | Defines a group of elements to be used in complex type definitions 定义了将用于复合类型说明的一组元素 |
import | Adds multiple schemas with different target namespace to a document 用不同的目标名称空间把多项schema添加到一份文件 |
include | Adds multiple schemas with the same target namespace to a document 用同一目标名称空间把多项schema添加到文件 |
key | Specifies an attribute or element value as a key (unique, non-nullable, and always present) within the containing element in an instance document 在实例文档包含的元素里指明某属性或元素值,使它们成为“钥匙”(特殊的,非零的,经常是存在的) |
keyref | Specifies that an attribute or element value correspond to those of the specified key or unique element 定义用于回应已指明关键或特殊元素的属性或元素值 |
list | Defines a simple type element as a list of values 一张值的列表的形式定义一个简单类型元素 |
notation | Describes the format of non-XML data within an XML document 描述在XML文档里的非XML形式数据的格式 |
redefine | Redefines simple and complex types, groups, and attribute groups from an external schema 从外部的schema中重新定义简单和复合类型,群,以及属性群 |
restriction | Defines restrictions on a simpleType, simpleContent, or a complexContent 在简单类型,简单文本或复合文本上定义约束 |
schema | Defines the root element of a schema 定义schema的根元素 |
selector | Specifies an XPath expression that selects a set of elements for an identity constraint 指明一段可为身份约束选择一组节点的XPath表达 |
sequence | Specifies that the child elements must appear in a sequence. Each child element can occur from 0 to any number of times 指明了子元素必须以一定顺序出现。每个子元素可以出现任意次 |
simpleContent | Contains extensions or restrictions on a text-only complex type or on a simple type as content and contains no elements 包含着作为只有文本的复合类型或简单类型的文本的扩展和约束,不包含元素 |
simpleType | Defines a simple type and specifies the constraints and information about the values of attributes or text-only elements 定义了简单类型,指定了关于属性值或只有文本的元素的信息 |
union | Defines a simple type as a collection (union) of values from specified simple data types 从已指定的简单数据类型里定义简单种类,作为值的集合 |
unique | Defines that an element or an attribute value must be unique within the scope 定义了在所属范围内一个元素或属性值必须是独特(没有重复的) |
XSD Restrictions/Facets for Datatypes
XSD对各种数据类型的约束
Look at XSD Restrictions!
下面看一看关于XSD的约束
Constraint 约束 |
Description 描述 |
---|---|
enumeration | Defines a list of acceptable values 定义了一系列可接受的属性的值 |
fractionDigits | Specifies the maximum number of decimal places allowed. Must be equal to or greater than zero 指明了所允许的最多小数位数。必须等于大于0 |
length | Specifies the exact number of characters or list items allowed. Must be equal to or greater than zero 指明了所允许的字符或列表项的确切个数。必须等于大于零 |
maxExclusive | Specifies the upper bounds for numeric values (the value must be less than this value) 指明了最大数值(数值必须小于最大数值) |
maxInclusive | Specifies the upper bounds for numeric values (the value must be less than or equal to this value) 指明了数值上限(数值必须小于等于数值上限) |
maxLength | Specifies the maximum number of characters or list items allowed. Must be equal to or greater than zero 指明了所允许的字符或列表项的最多个数。必须大于等于0 |
minExclusive | Specifies the lower bounds for numeric values (the value must be greater than this value) 指明了数值的下限(数值必须比这个值大) |
minInclusive | Specifies the lower bounds for numeric values (the value must be greater than or equal to this value) 指明了数值的下限(数值要大于等于这个值) |
minLength | Specifies the minimum number of characters or list items allowed. Must be equal to or greater than zero 指明了所允许的字符和列表项的最少个数。必须大于等于0个 |
pattern | Defines the exact sequence of characters that are acceptable 定义了可接受字符的确切顺序 |
totalDigits | Specifies the exact number of digits allowed. Must be greater than zero 定义了所允许的阿拉伯数字的确切个数。必须比0个多 |
whiteSpace | Specifies how white space (line feeds, tabs, spaces, and carriage returns) is handled 定义了各种空白区域(white space) (换行符、制表符、空格、回车符)是如何被处理的。 |