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,51)"><xs:element name="car">
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
The example above could also have been written like this:
上面的例子也可以写成这样:
<xs:element name="car" type="carType"/>
<xs:simpleType name="carType">
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:pattern value="[a-z]"/>
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:pattern value="[A-Z][A-Z][A-Z]"/>
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:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z]"/>
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,z三个字母中的任意一个
<xs:element name="choice">
<xs:pattern value="[xyz]"/>
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:pattern value="[0-9][0-9][0-9][0-9][0-9]"/>
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:pattern value="([a-z])*"/>
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:pattern value="([a-z][A-Z])+"/>
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:pattern value="male|female"/>
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:pattern value="[a-zA-Z0-9]{8}"/>
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:whiteSpace value="preserve"/>
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:whiteSpace value="replace"/>
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,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:whiteSpace value="collapse"/>
Restrictions on Length
对长度的约束
To limit the length of a value in an element,we would use the length,maxLength,and minLength constraints.
为了限制元素的长度值,我们会用length,和 minLength 约束。
This example defines an element called "password" with a restriction. The value must be exactly eight characters:
下面的例子给叫做"password"的元素定义了一个约束。值必须正好有8个字符:
<xs:length value="8"/>
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:minLength value="5"/>
<xs:maxLength value="8"/>
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,and carriage returns) is handled
指定了空白该怎样被处理(换行符,制表符,空格符和回车符)
XSD Complex 元素
w3pop.com / 2006-09-21
XSD Restrictions/Facets XSD Complex 空元素
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"/>
只含有其他元素的复合XML元素,"employee"
<employee>
<firstname>John</firstname>
<lastname>Smith</lastname>
</employee>
只含有文本的复合XML元素, "food":
<food type="dessert">Ice cream</food>
含有元素和文本的复合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,51)">看这个只含有其他元素的复合XML元素,"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>
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:complexType>
如果你用上述方法,几个元素指的可以是相同的复合类型,就像这样:
<xs:element name="student" type="personinfo"/>
<xs:element name="member" type="personinfo"/>
You can also base a complex element on an existing complex element and add some elements,51)">你也可以在现存的复合元素(Complex Elements)上再加上一个复合元素(Complex Elements),并添加一些元素,就像这样:
<xs:element name="employee" type="fullpersoninfo"/>
<xs:complexType name="fullpersoninfo">
<xs:complexContent>
<xs:extension base="personinfo">
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="country" type="xs:string"/>
</xs:extension>
</xs:complexContent>
XSD Complex 空元素
XSD Complex 元素 XSD 复合类型 - 纯元素
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,51)">上述"product"元素完全不含内容。为定义不含内容的类型,我们必须定义一个内容中只允许出现元素的类型,但我们不需要声明任何元素,就像这样:
<xs:element name="product">
<xs:attribute name="prodid" type="xs:positiveInteger"/>
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,51)">但是,可以更加简洁地声明"product"元素,就像这样:
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:complexType>