Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

XML Schema Restriction / Facets

XSD Restriction / Facets


Restriction is used to define acceptable values for an XML element or attribute. A restriction on an XML element is called a facet.


Restriction on Values

The following example defines an element named "age" with a restriction. The value of age cannot be lower than 0 or higher than 120:

  xs:simpleType

   

     

     

   

 


Restriction on a Set of Values

To restrict the content of an XML element to a set of acceptable values, we use an enumeration constraint.

The following example defines an element named "car" with a restriction. The only acceptable values are: Audi, Golf, BMW:

  xs:simpleType

   

     

     

     

   

 

The example above can also be written as:

 

   

   

   

 

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 Range of Values

To restrict the content of an XML element to a range of usable numbers or letters, we use a pattern constraint.

The following example defines an element named "letter" with a restriction. The only acceptable value is one of the lowercase letters a - z:

  xs:simpleType

   

     

   

 

The next example defines an element named "initials" with a restriction. The acceptable values are three of the uppercase letters A - Z:

  xs:simpleType

   

     

   

 

The next example also defines an element named "initials" with a restriction. The acceptable values are three uppercase or lowercase letters a - z:

  xs:simpleType

   

     

   

 

The next example defines an element named "choice" with a restriction. The acceptable value is one of the letters x, y, or z:

  xs:simpleType

   

     

   

 

The next example defines an element named "prodid" with a restriction. The acceptable value is a sequence of five digits, each in the range 0-9:

  xs:simpleType

   

     

   

 


Other Restrictions on a Range of Values

The following example defines an element named "letter" with a restriction. The acceptable value is zero or more letters from a - z:

  xs:simpleType

   

     

   

 

The following example defines an element named "letter" with a restriction. The acceptable value is 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:simpleType

   

     

   

 

The following example defines an element named "gender" with a restriction. The acceptable values are male or female:

  xs:simpleType

   

     

   

 

The following example defines an element named "password" with a restriction. The acceptable value is a line of 8 characters, which must be uppercase or lowercase letters a - z or digits from 0 - 9:

  xs:simpleType

   

     

   

 


Restriction on Whitespace Characters

To specify how whitespace characters are handled, we use the whiteSpace restriction.

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

  xs:simpleType

   

     

   

 

This example also defines an element named "address" with a restriction. This whiteSpace restriction is set to "replace", which means the XML processor will remove all whitespace characters (line feeds, carriage returns, spaces, and tabs):

  xs:simpleType

   

     

   

 

This example also defines an element named "address" with a restriction. This whiteSpace restriction is set to "collapse", which means the XML processor will remove all whitespace characters (line feeds, carriage returns, spaces, and tabs are replaced with spaces, leading and trailing spaces are removed, and multiple consecutive spaces are reduced to a single space):

  xs:simpleType

   

     

   

 


Restriction on Length

To limit the length of a value in an element, we use the length, maxLength, and minLength restrictions.

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

  xs:simpleType

   

     

   

 

This example also defines an element named "password" with a restriction. Its value must be a minimum of 5 characters and a maximum of 8 characters:

  xs:simpleType

   

     

     

   

 


Restriction on Data Types

Limit Description
enumeration Defines 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 bound for numeric values. Allowed values must be less than this value.
maxInclusive Defines the upper bound for numeric values. Allowed values 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 bound for numeric values. Allowed values must be greater than this value.
minInclusive Defines the lower bound for numeric values. Allowed values 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 the exact sequence of acceptable characters.
totalDigits Defines the exact number of digits allowed. Must be greater than 0.
whiteSpace Defines how whitespace characters (line feeds, carriage returns, spaces, and tabs) are handled.