XSD Facets

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

Value restriction

The following example defines an element named "age" with a restriction. The value of age must not 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>

Constraints 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 with a specified name 'car'. 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.

Constraints 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 with a specified name 'letter'. The acceptable values are 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 with a specified name 'initials'. 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 with a specified name 'initials'. 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 with a specified name 'choice'. 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 with a specified name 'prodid'. The acceptable values are a sequence of five Arabic numerals, each ranging 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 limitations on 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 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 numbers 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>

Limitation on 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限定. This whiteSpace限定 is set to 'replace', which means 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. This whiteSpace limitation is set to "collapse", which means that the XML processor will remove all whitespace characters (carriage returns, line feeds, 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 and must be exactly 8 characters in length:

<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 number. 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 characters or list items allowed. It must be greater than or equal to 0.
pattern Define the exact sequence of characters that are acceptable.
totalDigits Define the exact number of decimal places allowed for Arabic numerals. It must be greater than 0.
whiteSpace Define the handling of whitespace characters (newlines, carriage returns, spaces, and tabs).