XML Schema allows you to create new types by restricting the possible values of an XML
Schema simple type. For example, you can define a simple type, SSN
,
which is a string of exactly nine characters. New types defined by restricting simple types
are defined using a simpleType
element.
The definition of a type by restriction requires three things:
The name of the new type is specified by the
name
attribute of thesimpleType
element.The simple type from which the new type is derived, called the base type, is specified in the
restriction
element. See Specifying the base type.The rules, called facets, defining the restrictions placed on the base type are defined as children of the
restriction
element. See Defining the restrictions.
The base type is the type that is being restricted to define the new type. It is
specified using a restriction
element. The restriction
element is the only child of a simpleType
element and has one attribute, base
, that specifies the base
type. The base type can be any of the XML Schema simple types.
For example, to define a new type by restricting the values of an xsd:int you use a definition like the one shown in Example 2.14.
Example 2.14. Using int as the base type
<simpleType name="restrictedInt"> <restriction base="xsd:int"> ... </restriction> </simpleType>
The rules defining the restrictions placed on the base type are called
facets. Facets are elements with one attribute, value
, that defines how the facet is enforced. The available
facets and their valid value
settings depend on the base
type. For example, xsd:string supports six facets, including:
length
minLength
maxLength
pattern
whitespace
enumeration
Each facet element is a child of the restriction
element.
Example 2.15 shows an example of a simple type, SSN
,
which represents a social security number. The resulting type is a string of the form
xxx-xx-xxxx
. <SSN>032-43-9876<SSN> is a
valid value for an element of this type, but
<SSN>032439876</SSN> is not.
Example 2.15. SSN simple type description
<simpleType name="SSN"> <restriction base="xsd:string"> <pattern value="\d{3}-\d{2}-\d{4}"/> </restriction> </simpleType>