XSD Restrictions/Facets

We can use the restrictions to define the acceptable values for XML elements or attributes. Facets is the name given to the restrictions on XML elements.

Restrictions on Values:

Example:

<xs:element name="cost">
  <xs:simpletype>
    <xs:restriction base="xs:integer">
      <xs:mininclusive value="100"></xs:mininclusive>
      <xs:maxinclusive value="500"></xs:maxinclusive>
    </xs:restriction>
  </xs:simpletype>
</xs:element>

Explanation:

In the above example, we are defining an element called “cost” with a restriction, i.e., it cannot have a value lower than 100 or greater than 500.

Restrictions on a Set of Values:

The enumeration constraint can be used to limit the content of an XML element to a set of acceptable values.

Example 1:

<xs:element name="colour">
  <xs:simpletype>
    <xs:restriction base="xs:string">
      <xs:enumeration value="Red"></xs:enumeration>
      <xs:enumeration value="White"></xs:enumeration>
      <xs:enumeration value="Black"></xs:enumeration>
    </xs:restriction>
  </xs:simpletype>
</xs:element>

Explanation:

In the above example, we are defining an element called “colour” with a restriction, i.e., Red, White, and Black are the only acceptable values.

Example 2:

<xs:element name="colour" type="colourType"></xs:element>
 
<xs:simpletype name="colourType">
  <xs:restriction base="xs:string">
    <xs:enumeration value="Red"></xs:enumeration>
    <xs:enumeration value="White"></xs:enumeration>
    <xs:enumeration value="Black"></xs:enumeration>
  </xs:restriction>
</xs:simpletype>

Explanation:

In the above example, we are defining an element called “colour” with the same restrictions, however, the type “colourType”, here, can be used by other elements, because it is not a part of the “colour” element.

Restrictions on a Series of Values:

The pattern constraint can be used to limit the content of an XML element to define a series of numbers or letters that can be used.

Example 1:

<xs:element name="alphabets">
  <xs:simpletype>
    <xs:restriction base="xs:string">
      <xs:pattern value="[A-Z]"></xs:pattern>
    </xs:restriction>
  </xs:simpletype>
</xs:element>

Explanation:

In the above example, we are defining an element called “alphabets” with a restriction, i.e., the value can be one of the UPPERCASE letters from A to Z, only.

Example 2:

<xs:element name="four">
  <xs:simpletype>
    <xs:restriction base="xs:string">
      <xs:pattern value="[a-z][a-z][a-z][a-z]"></xs:pattern>
    </xs:restriction>
  </xs:simpletype>
</xs:element>

Explanation:

In the above example, we are defining an element called “four” with a restriction, i.e., the only acceptable value is four of the LOWERCASE letters from a to z.

Example 3:

<xs:element name="four">
  <xs:simpletype>
    <xs:restriction base="xs:string">
      <xs:pattern value="[a-zA-Z][a-zA-Z][a-zA-Z][a-zA-Z]"></xs:pattern>
    </xs:restriction>
  </xs:simpletype>
</xs:element>

Explanation:

In the above example, we are defining an element called “four” with a restriction, i.e., the only acceptable value is four of the LOWERCASE OR UPPERCASE letters from a to z.

Example 4:

<xs:element name="option">
  <xs:simpletype>
    <xs:restriction base="xs:string">
      <xs:pattern value="[abc]"></xs:pattern>
    </xs:restriction>
  </xs:simpletype>
</xs:element>

Explanation:

In the above example, we are defining an element called “option” with a restriction, i.e., the only acceptable value is one of the letters: a, b or c.

Example 5:

<xs:element name="num">
  <xs:simpletype>
    <xs:restriction base="xs:integer">
      <xs:pattern value="[0-9][0-9][0-9]"></xs:pattern>
    </xs:restriction>
  </xs:simpletype>
</xs:element>

Explanation:

In the above example, we are defining an element called “num” with a restriction, i.e., the only acceptable value is three digits in a sequence, where each digit is in a range from 0 to 9.

Other Restrictions on a Series of Values:

Example 1:

<xs:element name="alphabets">
  <xs:simpletype>
    <xs:restriction base="xs:string">
      <xs:pattern value="([A-Z])*"></xs:pattern>
    </xs:restriction>
  </xs:simpletype>
</xs:element>

Explanation:

In the above example, we are defining an element called “alphabets” with a restriction, i.e., the only acceptable value is zero or more occurrences of UPPERCASE letters from A to Z.

Example 2:

<xs:element name="alphabets">
  <xs:simpletype>
    <xs:restriction base="xs:string">
      <xs:pattern value="([a-z][A-Z])+"></xs:pattern>
    </xs:restriction>
  </xs:simpletype>
</xs:element>

Explanation:

In the above example, we are defining an element called “alphabets” with a restriction, i.e., the only acceptable value is one or more pairs of letters, each pair consisting of a LOWERCASE letter followed by an UPPERCASE letter. For example, “fOuR”, and not “FOUR” or “four” or “Four”.

Example 3:

<xs:element name="age">
  <xs:simpletype>
    <xs:restriction base="xs:string">
      <xs:pattern value="child|adult"></xs:pattern>
    </xs:restriction>
  </xs:simpletype>
</xs:element>

Explanation:

In the above example, we are defining an element called “age” with a restriction, i.e., the only acceptable value is child or adult.

Example 4:

<xs:element name="passcode">
  <xs:simpletype>
    <xs:restriction base="xs:string">
      <xs:pattern value="[a-zA-Z0-9]{7}"></xs:pattern>
    </xs:restriction>
  </xs:simpletype>
</xs:element>

Explanation:

In the above example, we are defining an element called “passcode” with a restriction, i.e., the seven characters in a row is a must and the characters must be lowercase or uppercase letters from a to z or a number from 0 to 9.

Restrictions on Whitespace Characters:

The whiteSpace constraint can be used to specify the way whitespace characters should be handled.

Example 1:

<xs:element name="password">
  <xs:simpletype>
    <xs:restriction base="xs:string">
      <xs:whitespace value="preserve"></xs:whitespace>
    </xs:restriction>
  </xs:simpletype>
</xs:element>

Explanation:

In the above example, we are defining an element called “password” with a restriction, i.e., the XML processor WILL NOT remove any white space characters, for which the whiteSpace constraint is set to “preserve”.

Example 2:

<xs:element name="password">
  <xs:simpletype>
    <xs:restriction base="xs:string">
      <xs:whitespace value="replace"></xs:whitespace>
    </xs:restriction>
  </xs:simpletype>
</xs:element>

Explanation:

In the above example, we are defining an element called “password” with a restriction, i.e., the XML processor WILL REPLACE all white space characters (line feeds, tabs, spaces, and carriage returns) with spaces, for which the whiteSpace constraint is set to “replace”.

Example 3:

<xs:element name="password">
  <xs:simpletype>
    <xs:restriction base="xs:string">
      <xs:whitespace value="collapse"></xs:whitespace>
    </xs:restriction>
  </xs:simpletype>
</xs:element>

Explanation:

In the above example, we are defining an element called “password” with a restriction, i.e., the XML processor will remove all white space characters. It means that the line feeds, tabs, spaces, carriage returns are replaced with spaces, leading and trailing spaces will be removed, and multiple spaces will be reduced to a single space. For this, the whiteSpace constraint is set to “collapse”.

Restrictions on Length:

The length, maxLength, and minLength constraints can be used to limit the length of a value in an element.

Example 1:

<xs:element name="passcode">
  <xs:simpletype>
    <xs:restriction base="xs:string">
      <xs:length value="10"></xs:length>
    </xs:restriction>
  </xs:simpletype>
</xs:element>

Explanation:

In the above example, we are defining an element called “passcode” with a restriction, i.e., the value must be exactly ten characters.

Example 2:

<xs:element name="passcode">
  <xs:simpletype>
    <xs:restriction base="xs:string">
      <xs:minlength value="7"></xs:minlength>
      <xs:maxlength value="10"></xs:maxlength>
    </xs:restriction>
  </xs:simpletype>
</xs:element>

Explanation:

In the above example, we are defining an element called “passcode” with a restriction, i.e., the value must be a minimum of seven characters and a maximum of ten characters.

Restrictions for Data Types:

ConstraintUses
enumerationUsed to define a list of acceptable values.
fractionDigitsUsed to specify the maximum number of decimal places allowed. Must be equal to or greater than zero.
lengthUsed to specify the exact number of characters or list items allowed. Must be equal to or greater than zero.
maxExclusiveUsed to specify the upper bounds for numeric values. The value must be less than this value.
maxInclusiveUsed to specify the upper bounds for numeric values. The value must be less than or equal to this value.
maxLengthUsed to specify the maximum number of characters or list items allowed. Must be equal to or greater than zero.
minExclusiveUsed to specify the lower bounds for numeric values. The value must be greater than this value.
minInclusiveUsed to specify the lower bounds for numeric values. The value must be greater than or equal to this value.
minLengthUsed to specify the minimum number of characters or list items allowed. Must be equal to or greater than zero.
patternUsed to specify the exact sequence of characters that are acceptable.
totalDigitsUsed to specify the exact number of digits allowed. Must be greater than zero.
whiteSpaceUsed to specify how white space i.e., line feeds, tabs, spaces, and carriage returns, is handled.
Please Share