對一組值的約束(使用枚舉)
下述案例給名為"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>
對一系列值的約束
下述案例給名為"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>
下述案例給名為"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>
對空白符的約束
下述案例給名為"address"的元素定義了一個約束條件。空白符設置為"preserve"(保留),這意味著XML處理器不會刪除任何空白符:
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="preserve"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
下述案例給名為"address"的元素定義了一個約束條件。空白符設置為" replace "(替代),這意味著XML處理器會用空格替代所有的空白字符(其中包括:換行符、制表符、空格符、回車符):
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="replace"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
下述案例給名為"address"的元素定義了一個約束條件。空白符設置為"collapse"(清除),這意味著XML處理器會清除所有的空白字符(換行符、制表符、空格符以及回車符都被空格符替代。頭部、尾部的空格會被清除,多個空格也會自動減少為一個):
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="collapse"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
對數據類型的約束
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 |
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 |
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 |
pattern |
Defines the exact sequence of characters that are acceptable |
totalDigits |
Specifies the exact number of digits allowed. Must be greater than zero |
whiteSpace |
Specifies how white space (line feeds, tabs, spaces, and carriage returns) is handled |