Restriction is used to define acceptable values for XML elements or attributes. The qualification of the XML element is called facet. The following example defines an element with a qualifier named “age”. The value of age cannot be less than 0 or higher than 120: To limit the content of the XML element to a set of acceptable values, we use an enumeration constraint (enumeration constraint). The following example defines an element with a qualified name “car”. The only acceptable values are Audi, Golf, BMW: The above example can also be written as: 注意: In this case, the type “carType” can be used by other elements because it is not part of the “car” element. To define the content limit of a XML element as a series of numbers or letters that can be used, we use a schema constraint (pattern constraint). The following example defines an element with a qualified name “letter”. Acceptable values are only one of the lowercase letters a-z: The next example defines an element with a qualified name “initials”. Acceptable values are three of the uppercase letters A-Z: The next example also defines an element with a qualified name “initials”. The acceptable values are three of the uppercase or lowercase letters a-z: The next example defines an element with a qualified name “choice. Acceptable values are one of the letters x, y, or z: The next example defines an element with a qualified name “prodid”. The acceptable value is a sequence of five Arabic numerals with a range of 0-9 for each number: The following example defines an element with a qualified name “letter”. Acceptable values are zero or more letters in a-z: The following example defines an element with a qualified name “letter”. Acceptable values are one or more pairs of letters, each consisting of a lowercase letter followed by an uppercase letter. For example, “sToP” will pass the validation of this pattern, but “Stop”, “STOP” or “stop” will not: The following example defines an element with a qualified name “gender”. Acceptable values are male or female: The following example defines an element with a qualified name “password”. Acceptable values are a line of 8 characters that must be uppercase or lowercase letters a-z or the number 0-9: To specify how white space characters (whitespace characters) are handled, we need to use whiteSpace qualification. The following example defines an element with a qualified name “address”. This whiteSpace qualification is set to “preserve”, which means that the XML processor does not remove any white space characters: This example also defines an element with a qualified name “address”. This whiteSpace qualification is set to “replace”, which means that the XML processor removes all white space characters (line feeds, carriage returns, spaces, and tabs): This example also defines an element with a qualified name “address”. This whiteSpace qualification is set to “collapse”, which means that the XML processor will remove all white space characters (line feeds, carriage returns, spaces, and tabs will be replaced with spaces, spaces at the beginning and end will be removed, and multiple consecutive spaces will be reduced to a single space): To limit the length of the value in an element, we need to use length, maxLength, and minLength to qualify. This example defines an element with a qualifier named “password”. The value must be accurate to 8 characters: This example also defines an element with a qualified name “password”. The minimum value is 5 characters and the maximum is 8 characters: Limit Description Enumeration Define a list of acceptable values FractionDigits Defines the maximum number of decimal places allowed. Must be greater than or equal to 0. Length Defines the exact number of characters or list items allowed. Must be greater than or equal to 0. MaxExclusive Defines the upper limit of the value. The allowed value must be less than this value. MaxInclusive Defines the upper limit of the value. The allowed value must be less than or equal to this value. MaxLength Defines the maximum number of characters or list items allowed. Must be greater than or equal to 0. MinExclusive Defines the lower limit of the value. The allowed value must be greater than this value. MinInclusive Defines the lower limit of the value. The allowed value must be greater than or equal to this value. MinLength Defines the minimum number of characters or list items allowed. Must be greater than or equal to 0. Pattern Defines a precise sequence of acceptable characters. TotalDigits Defines the exact number of digits allowed for Arabic numerals. Must be greater than 0. WhiteSpace Defines how white space characters (line feeds, carriage returns, spaces, and tabs) are handled.Limitation of value ¶
<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>
The qualification of a set of values. ¶
<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>
<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>
The limitation of a series of values. ¶
<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-z]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<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>
<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>
<xs:element name="choice">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[xyz]"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<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 ¶
<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z])*"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="letter">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="([a-z][A-Z])+"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="gender">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="male|female"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<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>
The limitation of white space characters ¶
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="preserve"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="replace"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="collapse"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
The limitation of length ¶
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<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>
Qualification of data type ¶