XSD Facets

Restriction (restriction) is used to define acceptable values for XML elements or attributes. The restriction on XML elements is called a facet.

Restriction on values

The following example defines an element named "age" with a restriction. The value of "age" cannot be less than 0 or greater than 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>

Restriction on a set of values

To limit the content of an XML element to a set of acceptable values, we need to use enumeration constraints.

The following example defines an element named "car" with a restriction. The acceptable values are only 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 above example can also be written as:

<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 part of the "car" element.

Restriction on a series of values

To limit the content of an XML element to a series of usable numbers or letters, we need to use pattern constraints.

The following example defines an element named "letter" with a restriction. The acceptable value is only one lowercase 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 named "initials" with a restriction. The acceptable values are three uppercase letters A-Z:

<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 named "initials" with a restriction. The acceptable values are three uppercase or lowercase letters 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 named 'choice' with a限定. The acceptable values are one of the letters x, y, or 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 named 'prodid' with a限定. The acceptable values are a sequence of five Arabic numerals, and each digit ranges from 0 to 9:

<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限定 of a series of values

The following example defines an element named 'letter' with a限定. The acceptable values are zero or more letters from 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 following example defines an element named 'letter' with a限定. The acceptable values are one or more pairs of letters, each pair consisting of a lowercase letter followed by an uppercase letter. For example, 'sToP' will pass this pattern's validation, but 'Stop', 'STOP', or 'stop' will not:

<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 following example defines an element named 'gender' with a限定. The acceptable values are 'male' or 'female':

<xs:element name="gender">
<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:pattern value="male|female"/>
  </xs:restriction>
</xs:simpleType>
</xs:element>

The following example defines an element named 'password' with a限定. The acceptable values are a line of characters consisting of 8 characters, which must be uppercase or lowercase letters a - z or digits 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>

The限定 of whitespace characters

To specify the handling of whitespace characters, we need to use the whiteSpace限定.

The following example defines an element named 'address' with a限定. This whiteSpace限定 is set to 'preserve', which means the XML processor will not remove any whitespace characters:

<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 named "address" with a limitation. The whiteSpace limitation is set to "replace", which means that the XML processor will remove all whitespace characters (newlines, carriage returns, spaces, and tabs):

<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 named "address" with a limitation. The whiteSpace limitation is set to "collapse", which means that the XML processor will remove all whitespace characters (newlines, carriage returns, spaces, and tabs will be replaced with a space, leading and trailing spaces will be removed, and multiple consecutive spaces will be reduced to a single space):

<xs:element name="address">
<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:whiteSpace value="collapse"/>
  </xs:restriction>
</xs:simpleType>
</xs:element>

Limitation on length

To limit the length of the value within an element, we need to use the length, maxLength, and minLength limitations.

This example defines an element named "password" with a limitation. Its value must be exactly 8 characters long:

<xs:element name="password">
<xs:simpleType>
  <xs:restriction base="xs:string">
    <xs:length value="8"/>
  </xs:restriction>
</xs:simpleType>
</xs:element>

This example also defines an element named "password" with a limitation. Its value must be between 5 and 8 characters in length:

<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>

Limitation of data type

Limitation Description
enumeration Define a list of acceptable values
fractionDigits Define the maximum number of decimal places allowed. It must be greater than or equal to 0.
length Define the exact number of allowed characters or list items. It must be greater than or equal to 0.
maxExclusive Define the upper limit of the value. The allowed values must be less than this value.
maxInclusive Define the upper limit of the number. The allowed values must be less than or equal to this value.
maxLength Define the maximum number of allowed characters or list items. It must be greater than or equal to 0.
minExclusive Define the lower limit of the number. The allowed values must be greater than this value.
minInclusive Define the lower limit of the number. The allowed values must be greater than or equal to this value.
minLength Define the minimum number of allowed characters or list items. It must be greater than or equal to 0.
pattern Define the precise sequence of acceptable characters.
totalDigits Define the precise number of decimal places allowed for the Arabic numerals. It must be greater than 0.
whiteSpace Define the processing method for whitespace characters (new lines, carriage returns, spaces, and tab characters).