当前位置: 首页 > 网络学院 > XML相关教程 > Schema (XSD) > XSD 约束面

Schema (XSD)
Schema (XSD) 介绍
为何使用 XML Schemas?
如何定制 XSD
XSD - <schema>元素
XSD 简单元素
XSD 属性
XSD 约束面
XSD 复合元素
XSD 复合空元素
XSD 复合纯元素
XSD 复合纯文本
XSD 混合内容的复合类型
XSD 指示器复合类型
XSD <any> 元素
XSD <anyAttribute> 元素
XSD 元素替代
XSD 实例
XSD 字符串数据类型
XSD 日期数据类型
XSD 小数数据类型

Schema (XSD) 中的 XSD 约束面


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 849 ::
收藏到网摘: n/a

Restrictions are used to define acceptable values for XML elements or attributes. Restrictions on XML elements are called facets.
约束是用于为XML元素或属性定义可接受的值。对于XML元素的约束称之为“约束面”。


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"的元素定义了一个“约束”。“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元素的内容只接受一组符合条件的值,我们会使用“列举约束”。

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元素的内容,我们只定义了一系列可被XML内容使用的数字或字母。为了实现上述目标,我们可以用“式样约束”。

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 的小写字母(可以不包含任何字母或包含多个字母):

<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
指定了空白符的处理方式(其中包括:换行符、制表符、空格符和回车符)

评论 (0) All

登陆 | 还没注册?