A simple type describes text without markup. A simple type can be assigned either to an element or to an attribute. A simple type is either a primitive built-in simple type, whose meaning is predefined, or a derived simple type, which is defined in terms of other simple types.
Some typical examples of primitive built-in simple types are: string, integer, decimal, float, double, boolean, time, and date. Derived types can be defined in the following three ways:
Facet | Constraint |
---|---|
length | length of string or number of items of list |
minLength | minimal length |
maxLength | maximal length |
pattern | regular expression pattern |
enumeration | enumeration value |
whiteSpace | white space normalization |
maxInclusive | inclusive upper bound |
maxExclusive | exclusive upper bound |
minInclusive | inclusive lower bound |
minExclusive | exclusive lower bound |
totalDigits | maximum number of digits |
fractionDigits | maximum number of fractional digits |
<xs:simpleType name="voteType"> <xs:restriction base="xs:integer"> <xs:minInclusive value="18"/> <xs:maxInclusive value="30"/> </xs:restriction> </xs:simpleType>As another example, the following type definition defines a price as a decimal number with maximum four digits in total where at most two of them are fractional. Accepted values are 18.98, 1.7 but also 018.980, since the constraints operate on the semantics of the values and not on their syntax:
<xs:simpleType name="priceType"> <xs:restriction base="xs:decimal"> <xs:totalDigits value="4"/> <xs:fractionDigits value="2"/> </xs:restriction> </xs:simpleType>The following example shows an enumeration simple type. The type colorType is the subset of strings containing red, green, and blue values only.
<xs:simpleType name="colorType"> <xs:restriction base="xs:string"> <xs:enumeration value="red"/> <xs:enumeration value="green"/> <xs:enumeration value="blue"/> </xs:restriction> </xs:simpleType>It is possible to constrain strings to belong to the set defined by a regular expression. For instance, the following type defines strings containing the word ace or starting with the word lov:
<xs:simpleType name="patternType"> <xs:restriction base="xs:string"> <xs:pattern value=".*ace.*|lov.*"/> </xs:restriction> </xs:simpleType>Regular expression syntax is similar to that used in Perl. The main operators are summarized in the following table, where A and B are expressions, n and m are natural numbers, and a, b, c, d, e, f are single Unicode characters:
Pattern | Meaning |
---|---|
(A) | parenthesis |
AB | concatenation |
A|B | union |
A? | zero or one occurrences |
A+ | one or more occurrences |
A* | zero or more occurrences |
A{n,m} | between n and m occurrences |
A{n} | exactly n occurrences |
A{n,} | at least n occurrences |
[abcd] | exactly one of the listed characters |
[^abcd] | exactly one character except those listed |
[a-d] | exactly one character between a and d |
[a-f-[c-e]] | exactly one character between a and f excluding characters between c and e |
\n | newline |
\r | carriage return |
\t | tab |
. | any character except carriage return and line feed |
\s | a space, tab, carriage return, or line feed |
\S | any character different from \s |
\d | a decimal digit |
\D | any character different from \d |
\w | a word character |
\W | any character different from \w |
<xs:simpleType name="intListType"> <xs:list itemType="xs:integer"/> </xs:simpleType> <xs:simpleType name="threeIntListType"> <xs:restriction base="intListType"> <xs:length value="3"/> </xs:restriction> </xs:simpleType>
<xs:simpleType name="stringColorType"> <xs:restriction base="xs:string"> <xs:enumeration value="red"/> <xs:enumeration value="green"/> <xs:enumeration value="blue"/> </xs:restriction> </xs:simpleType> <xs:simpleType name="intColorType"> <xs:restriction base="xs:integer"> <xs:enumeration value="1"/> <xs:enumeration value="2"/> <xs:enumeration value="3"/> </xs:restriction> </xs:simpleType> <xs:simpleType name="colorType"> <xs:union memberTypes="stringColorType intColorType"/> </xs:simpleType>