Designing Your own XML Schema: Restrictions and User Defined Types
This is the third article in a series which guides you in designing XML Schemas. Hopefully this series will take you from the most basic ideas to the more advanced topics without any hurdles.
Designing Your own XML Schema: Restrictions and User Defined Types - Restrictions based on patterns in XML Schema (Page 2 of 7 )
Let us consider that we would like to have an employee ID to be provided with only five digits, in the form of a string. If it is in the form of a string, it would accept any character. I wanted to restrict it to only digits. Another good example along the same lines would be a zip code.
In these situations, you can use “patterns” in XML Schema. Even though I present a simple example of this, I suggest you refer to http://www.w3c.org/ for further in-depth information on “patterns.” It is just beyond the scope of this article to concentrate so deeply on patterns.
Let us look at the following complete schema before I give a complete explanation:
You need to observe the following fragment within the above complete schema, which actually does the restriction:
<xs:element name="ID">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]{5}" />
</xs:restriction>
</xs:simpleType>
</xs:element>
The above “restriction” is of type “pattern” facet where it would allow any digit between zero and nine, and a maximum of five digits. And not only that, it would not accept less than five digits as well!
I strongly suggest you go through several types of patterns available in XML Schema criteria, before designing a schema. This will help you get a better grasp of what you need to know to design powerful schema.