XSD Restrictions/Facets

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
说明
enumerationDefines a list of acceptable values
定义了一系列的有效值
fractionDigitsSpecifies the maximum number of decimal places allowed. Must be equal to or greater than zero
指定了允许的小数位数的最多位数。必须大于等于0
lengthSpecifies the exact number of characters or list items allowed. Must be equal to or greater than zero
指定了允许的字符或列表项的个数。必须大于等于0
maxExclusiveSpecifies the upper bounds for numeric values (the value must be less than this value)
指定了数值的上限(数值要比这个值小)
maxInclusiveSpecifies the upper bounds for numeric values (the value must be less than or equal to this value)
指定了数值上限(数值必须小于等于这个值)
maxLengthSpecifies the maximum number of characters or list items allowed. Must be equal to or greater than zero
指定了所允许的字符或列表项的最多个数。必须大于等于0
minExclusiveSpecifies the lower bounds for numeric values (the value must be greater than this value)
指定了数值的下限 (数值要比这个值小)
minInclusiveSpecifies the lower bounds for numeric values (the value must be greater than or equal to this value)
指定了数值的下限(数值必须大于等于这个值)
minLengthSpecifies the minimum number of characters or list items allowed. Must be equal to or greater than zero
指定了所允许的字符或列表的最少个数。必须等于大于0个
patternDefines the exact sequence of characters that are acceptable
定义了符合要求的字符的确切排列顺序
totalDigitsSpecifies the exact number of digits allowed. Must be greater than zero
指定了所允许的字符的确切个数。必须大于0
whiteSpaceSpecifies how white space (line feeds, tabs, spaces, and carriage returns) is handled
指定了空白该怎样被处理(换行符,制表符,空格符和回车符)
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值