Designing Your own XML Schema: Constraining with Restrictions - Constraining values (restricting a range) (Page 3 of 6 )
Those who design databases should definitely know about “constraints.” It is the most important aspect of “data integrity” in any of the relational databases. A constraint is basically a type of “condition” to accept proper values. Fox example, employee ids should never repeat; age cannot be more than 120; salary cannot be more than $10,000 per month; and so on.
Such types of conditions are quite common in any scenario and especially in databases. They are common not only for databases, but also quite common for XML Schemas!
Now let us work with a simple practical scenario. I would like to have an element “AGE” be present in my XML Schema (very similar to the examples in the previous sections). I can assign a data type “integer” to it. But this is not sufficient, because I would like to have the value restricted to between 0 and 100 (whereas an integer would accept more than that). This means a “restriction” is necessary.
Let us see the XML Schema supporting the age “restriction:”
Let us concentrate on the “age” element. It stays under the “Employee” element. Its “simpleType” (or data type) is defined as an integer type. And finally, we applied the “restriction” by specifying minimum and maximum values as follows:
<xs:minInclusive value="0" />
<xs:maxInclusive value="100" />
We can also modify the above fragment to accept values between 1 and 99 (both inclusive) as follows:
<xs:minExclusive value="0" />
<xs:maxExclusive value="100" />
You can use “Exclusive” or “Inclusive” according to your requirements (they can even be interchanged). But care should be taken while defining the same (using tools).