Restrictions on a Set of Values
To limit the content of XML elements to a set of acceptable values, we would use the enumeration constraint.
The example below define an element called "car" with a restriction. The only acceptable values are: 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 example above could also have been written in like this:
<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 example above the type "carType" can be used by other elements because
it is not a part of the "car" element.
Restrictions on a Series of Values
To limit the content of an XML element to define a series of letters or numbers that can be used,
we would use the pattern constraint.
The example below define an element called "letter" with a restriction.
The only acceptable value is ONE of the LOWERCASE letter from a to 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 define an element called "initials" with a restriction.
The only acceptable value is THREE of the UPPERCASE letter from a to 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 define an element called "initials" with a restriction.
The only acceptable value is THREE of the LOWERCASE OR UPPERCASE letter from a to 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 define an element called "choice" with a restriction.
The only acceptable value is ONE of the following letter: 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 define an element called "prodid" with a restriction.
The only acceptable value is FIVE digits in a sequence, and each digit must be in range 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>
Restrictions on Whitespace Characters
To specify how whitespace characters should be handled, we
would use the whiteSpace constraints.
This example define an
element called "address" with a restriction. The whiteSpace
constraint is set to "preserve", which means that the XML
processor WILL NOT remove any white space character
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="preserve"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
This example also define an element called "address" with a restriction.
The whiteSpace constraint is set to "replace", which mean that the XML processor
WILL REPLACE all white space characters (line feeds, tabs, spaces, and carriage returns) with spaces:
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="replace"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
This example also define an element called "address" with a restriction.
The whiteSpace constraint is set to "collapse", which means that the XML processor
WILL REMOVE all white space character (line feeds, tabs, spaces, carriage returns are replaced with spaces,
leading and trailing spaces are removed, and multiple spaces are reduced to the single space):
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="collapse"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Restrictions on Length
To limit the length of a value in to an element, we would use
the length, maxLength, and minLength constraints.
This example
define an element called "password" with a restriction. The
value must be exactly eight characters:
<xs:element name="password">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="8"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
This example define another element called "password" with a restriction.
The value must be maximum eight characters and minimum five characters :
<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>
Restrictions for Datatypes
Constraint
Description
enumeration
Defines a list of acceptable values
fractionDigits
Specifies the maximum number of decimal places allowed.
Must be equal to or greater than zero
length
Specifies the exact number of characters or list items
allowed. Must be equal to or greater than zero
maxExclusive
Specifies the upper bounds for numeric values (the
value must be less than this value)
maxInclusive
Specifies the upper bounds for numeric values (the
value must be less than or equal to this value)
maxLength
Specifies the maximum number of characters or list
items allowed. Must be equal to or greater than zero
minExclusive
Specifies the lower bounds for numeric values (the
value must be greater than this value)
minInclusive
Specifies the lower bounds for numeric values (the
value must be greater than or equal to this value)
minLength
Specifies the minimum number of characters or list
items allowed. Must be equal to or greater than zero
pattern
Defines the exact sequence of characters that are
acceptable
totalDigits
Specifies the exact number of digits allowed. Must be
greater than zero
whiteSpace
Specifies how white space (line feeds, tabs, spaces,
and carriage returns) is handled
Be the first one to comment on this page.
Share And Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
Keywords:
XSD restrictions/facets,web service xsd,xsd tutorial,xsd xml,xsd examples,xsd dtd,xsd validator,xslt xsd,sample xsd