User-derived Simple Types
A schema author can derive a new simple type using the
<xs:simpleType> element. This simple type can then be used in the same way that built-in simple types are.
Simple types are derived by restricting built-in simple types or other user-derived simple types. For example, you might want to create a simple type called password that is an eight-character string. To do so, you would start with the
xs:string type and restrict its length to eight characters. This is done nesting the
<xs:restriction> element inside of the
Simple types can be derived by applying one or more of the following facets.
The length of a string can be controlled with the
maxLength facets. We used the
length facet in the example above to create a
Password simple type as an eight-character string. We could use
maxLength to allow passwords that were between six and twelve characters in length.
The schema below shows how this is done. The two XML instances shown below it are both valid, because the length of the password is between six and twelve characters.
Patterns are specified using the
xs:pattern element and regular expressions. For example, you could use the
xs:pattern element to restrict the Password simple type to consist of between six and twelve characters, which can only be lowercase and uppercase letters and underscores.
Numeric simple types can be derived by limiting the value to a certain range using
maxInclusive. You can also limit the total number of digits and the number of digits after the decimal point using
Mins and Maxs
The following example shows how to derive a simple type called
Salary, which is a decimal between 10,000 and 90,000.
fractionDigits, we can further specify that the
Salary type should consist of seven digits, two of which come after the decimal point. Both
fractionDigits are maximums. That is, if
totalDigits is specified as 7 and
fractionDigits is specified as 2, a valid number could have no more than seven digits total and no more than two digits after the decimal point.
A derived type can be a list of possible values. For example, the
JobTitle element could be a list of pre-defined job titles.
By default, whitespace in elements of the datatype
xs:string is preserved in XML documents; however, this can be changed for datatypes derived from
xs:string. This is done with the
xs:whiteSpace element, the value of which must be one of the following.
preserve - whitespace is not normalized. That is to say, it is kept as is.
replace - all tabs, line feeds, and carriage returns are replaced by single spaces.
collapse - all tabs, line feeds, and carriage returns are replaced by single spaces and then all groups of single spaces are replaced with one single space. All leading and trailing spaces are then removed (i.e, trimmed).
In SimpleTypes/Demos/Password.xsd, we looked at restricting the
length of a
Password datatype to eight characters using the
xs:length element. If whitespace is preserved, then leading and trailing spaces are considered part of the password. In the following example, we set
xs:whiteSpace to collapse, thereby discounting any leading or trailing whitespace. As you can see, this allows the XML instance author to format the document without consideration of whitespace.