∟"simpleType" Components with "restriction" Child Components
This section describes the 'restriction' child component in 'simpleType' component. 'restriction' child components are used to reduce the value space on a base datatype by changing its constraining facets.
In earlier tutorials, we learned datatype facets can be modified to create new datatypes using "restriction" components.
Now let's take closer look at the "restriction" component.
Here is an XSD template showing you how to use the "restriction" as a child component in a "simpleType" component:
Format 1:
<simpleType ...>
<restriction base="base_type">
facet component
facet component
...
</restriction>
</simpleType>
Format 2:
<simpleType ...>
<restriction>
<simpleType ...>
definition of the base datatype
</simpleType>
facet component
facet component
...
</restriction>
</simpleType>
Some notes on using the "restriction" component:
base="base_type" - Optional attribute to specify a name to the base datatype.
If not provided, the base datatype must be defined with "simpleType" as the first child.
The base datatype must be a simple datatype that supports one or more constraining facets.
Each facet component must make the facet more restrictive than the base datatype, or at least unchanged.
The same facet can only be specified once, except for "enumeration".
For example, the following XSD document uses "restriction" components to define 2 new simple datatypes:
When validating this XML document with my XsdSchemaValidator.java program presented earlier in the book,
I get these errors:
c:\Progra~1\Java\jdk1.7.0_07\bin\java XsdSchemaValidator
restriction_simpleType_test.xsd restriction_simpleType_test.xml
Error:
Line number: 11
Column number: 31
Message: cvc-pattern-valid: Value '::1:' is not facet-valid with
respect to pattern '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}' for type
'#AnonType_IPAddressRestriction_SimpleType_Test'.
Error:
Line number: 11
Column number: 31
Message: cvc-type.3.1.3: The value '::1:' of element 'IPAddress' is
not valid.
Error:
Line number: 17
Column number: 21
Message: cvc-enumeration-valid: Value '8080' is not facet-valid
with respect to enumeration '[80, 23]'. It must be a value from the
enumeration.
Error:
Line number: 17
Column number: 21
Message: cvc-type.3.1.3: The value '8080' of element 'Port' is not
valid.
Failed with errors: 4