Next page Previous page Start of chapter End of chapter

Simple types

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:

Restriction
A restriction of an existing type, called based type, defines a new type by restricting the set of possible values of the base type. The restriction can be performed on the facets in the following table:



FacetConstraint
lengthlength of string or number of items of list
minLengthminimal length
maxLengthmaximal length
patternregular expression pattern
enumerationenumeration value
whiteSpacewhite space normalization
maxInclusiveinclusive upper bound
maxExclusiveexclusive upper bound
minInclusiveinclusive lower bound
minExclusiveexclusive lower bound
totalDigitsmaximum number of digits
fractionDigitsmaximum number of fractional digits




For instance, the following type definition uses the simpleType element to define the simple type voteType to be an integer from 18 to 30:
<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:



PatternMeaning
(A)parenthesis
ABconcatenation
A|Bunion
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
\nnewline
\rcarriage return
\ttab
.any character except carriage return and line feed
\sa space, tab, carriage return, or line feed
\Sany character different from \s
\da decimal digit
\Dany character different from \d
\wa word character
\Wany character different from \w




Characters used in syntax (like | or *) can be escaped with a backslash sign (\).

Finally, the whiteSpace facet controls how the parser will deal with any whitspace within the target data. Whitespace normalization occurs before any of the other facets are processed. The possible values are: preserve (keep all whitespace), replace (replace tab, line feed and carrage return with space), and collapse (first perform the replace operation and then collapse strings of spaces into a single space).
List
A simple type may be a list of elements having atomic simple type. In particular, it is not possible to create lists of lists. In the following we define a list of integers and a list of integers of length three:
<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>
Union
A simple type may be defined as the union of simple types, as in the following example:
<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>
Next page Previous page Start of chapter End of chapter
Caffè XML - Massimo Franceschet