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

```

```

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:

```

```

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:

```

```

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:

```

```

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:

```

```

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:

```

```

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:

```

```

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:

```

```

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:

```

```

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:

```

```

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:

```

```

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:

```

```

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:

```

```

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:

```

```

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:

```

```

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:

```

```

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:

```

```

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:

 Constraint Uses enumeration Used to define a list of acceptable values. fractionDigits Used to specify the maximum number of decimal places allowed. Must be equal to or greater than zero. length Used to specify the exact number of characters or list items allowed. Must be equal to or greater than zero. maxExclusive Used to specify the upper bounds for numeric values. The value must be less than this value. maxInclusive Used to specify the upper bounds for numeric values. The value must be less than or equal to this value. maxLength Used to specify the maximum number of characters or list items allowed. Must be equal to or greater than zero. minExclusive Used to specify the lower bounds for numeric values. The value must be greater than this value. minInclusive Used to specify the lower bounds for numeric values. The value must be greater than or equal to this value. minLength Used to specify the minimum number of characters or list items allowed. Must be equal to or greater than zero. pattern Used to specify the exact sequence of characters that are acceptable. totalDigits Used to specify the exact number of digits allowed. Must be greater than zero. whiteSpace Used to specify how white space i.e., line feeds, tabs, spaces, and carriage returns, is handled.