![]()
Strong typing is one of the greatest strengths of Schema over DTD. As we know DTDs have little or no concept of datatypes. If one seriously wanted to create a type hierarchy for XML documents based on DTDs one probably could, but in a very unnatural way that would certainly need proprietary processing as well.
Schemas on the contrary, provides for strong typing naturally. Types in schema come in two forms:
• Simple Types: Type of element that only contain unstructured text
• Complex Types: Type of element that contains sub-elements or attributes
In this chapter we will examine Simple Types in some detail, while in the next we will examine Complex Types.
As we have seen above, Simple Type elements are those that only contain unstructured text. Before we jump to see what these type are and how they are defined, let us see how to declare an element to be a Simple Type.
Consider the following examples of element:
<xs:element name="PUBLICATIONDATE" type="xs:date"/>In the first example, the element PUBLICATIONDATE is declared to be of the
date type.
In the next example, the element AUTHOR is declared to be of the
string type.
Thus the type is essentially specified as an attribute of the element with the attribute name as the keyword “type” and the attribute value as the name of the simple type.
There is a rich built-in data type set defined by the XML Schema specification. Apart from these built-in types, one can also build customized simple types using certain special elements and notation.
XML Schema defines a rich set of built-in Simple Types. Some of the common ones are listed below.
| Type | Description |
| xs:string | Character Strings (has a number of built-in types) |
| xs:date | Calendar Date |
| xs:time | Daily recurring instant of time |
| xs:decimal | Arbitrary precision on decimal numbers |
| xs:Boolean | Boolean type |
Type Description xs:string Character Strings (has a number of built-in derived types) xs:date Calendar Date xs:time Daily recurring instant of time xs:decimal Arbitrary precision on decimal numbers xs:Boolean Boolean value This is by no means an exhaustive list but it does represent important segment types. The complete simple type hierarchy can be found at its source: http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#built-in-datatypes Some of the types shown above form parents for descendant derived types. For instance, xs:decimal is the parent type for the derived type xs:integer xs:string is the parent type for the derived type xs:NMTOKEN Let us therefore look at the concept of derived types next.
Derived types is a very powerful concept within schemas, that provides for type extensibility. The syntax for deriving a type is as follows:
<xs:simpleType name="typeName">
<xs:restriction base="someSimpleType">
Deriving Rule
</xs:restriction>
</xs:simpleType>
The xs:simpleType is basic definition of a derived simple type element. Within the derived type, the xs:restriction element is typically used to specify that a derivation is being done to a particular “base” type. Finally, within the xs:restriction element lie the actual deriving rules. Let us look at some common deriving rules with examples:
| Deriving Rule | Rule Example | Description |
| Pattern Helps define possible patterns of values |
<xs:element name="PHONE"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="\d{3}-\d{7}"/> </xs:restriction> </xs:simpleType> </xs:element> |
Patten Types (not exhaustive) (mn)* : 0 or more of mn’s (mn)? : 0 or 1 mn’s (mn)+ : 1 or more mn’s [mno] : 1 of a set comprising m, n or o \d : any digit \D: any non-digit \s : any whitespace character \S : any non whitespace character . : any character c{x,y} : At least “x” instances and at most “y” instances of c where c can be any pattern type if enclosed in () |
| Valid Phone Number: 816-6724567 Invalid Phone Number: 81-6724567 |
||
| Range Helps defines the range of any element | <xs:element name="AGE">
<xs:simpleType> <xs:restriction base="xs:integer"> <xs:minInclusive value="1"/> <xs:maxExclusive value="100"/> </xs:restriction> </xs:simpleType> </xs:element> |
Uses the elements below minInclusive : Equivalent to “>=” minExclusive: Equivalent to “>” maxInclusive: Equivalent to “<=” maxExclusive: Equivalent to “<” to limit the range of the element. |
| Valid Age: 67, 1, 99 Invalid Age: 0 or 100 or -1 | ||
| Enumeration Forcing contents of an elements to be members of a predefined set | <xs:element name="RGBCOLOR">
<xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="RED"/> <xs:enumeration value="GREEN"/> <xs:enumeration value="BLUE"/> </xs:restriction> </xs:simpleType> </xs:element> |
Uses the “xs:enumeration” element to restrict the selection of values from a predefined set of values only. |
| Valid RGB Color: RED, GREEN Invalid RGB Color: PURPLE, 4, PINK | ||
| Limiting Lengths
Strings
|
<xs:element name="COUNTRYCODE">
<xs:simpleType> <xs:restriction base="xs:string"> <xs:length value="2"/> </xs:restriction> </xs:simpleType> </xs:element> |
For strings the element: <xs:length value=”somevalue”> is used to restrict the length of the string |
| Valid Country Code: US, UK, SG Invalid Country Code USA, GBR, SIN | ||
| Numbers | <xs:element name="PRICE">
<xs:simpleType> <xs:restriction base="xs:decimal"> <xs:precision value="5"/> <xs:scale value=”2”/> </xs:restriction> </xs:simpleType> </xs:element> . |
For numbers the elements: <xs:precision value=”somevalue”> and <xs:scale value=”somevalue”> are used to restrict the total number of digits and the decimal places right of the dot respectively |
| Valid Price: 100.56, 9.9 Invalid Price: 65.679, 54321.00 |
There are three other categories of modifications with respect to Simple Types:
These are discussed below:
| Rule | Rule Example | Description |
| Lists: Allows not just single elements but also lists of elements | <xs:element
name="topfivesalaries">
<xs:simpleType> <xs:list base="xs:integer"/> <xs:length value=”5”> </xs:simpleType> </xs:element> |
Allows the creation of a single element with multiple values using: <xs:list base=”someType”> to define the list, and optionally <xs:length value=”someValue”> to constrain the number of elements |
| For example:
<topfivesalaries> 22000 20700 20300 18670 15550 </topfivesalaries> |
||
| Defaults: Specifies a default value for an element | <xs:element name="eng_degree" type=”xs:string” default=”BS”> | The “default” attribute as the name suggests, specifies a default value for the element and this rule kicks in when the element is empty or left out, in which case an element with the default value is automatically assumed. |
| Valid Elements: <eng_degree>BS</eng_degree> <eng_degree>BE</eng_degree> | ||
| Constants Specifies a constant value for the element | <xs:element name="eng_degree" type=”xs:string” fixed=”BS”> | The “fixed” attribute as the name suggests, specifies a constant value for the element. This element cannot appear with any other value. Also, while the element can be omitted the fixed value will not be assumed. |
| Valid Element:
<eng_degree>BS</eng_degree> Invalid Element: <eng_degree>BE</eng_degree> |
Simple types can be named or unnamed. Consider the following two definitions:
<xs:element name="AGE">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive
value="1"/>
<xs:maxExclusive
value="100"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:simpleType name="ageType">
<xs:restriction base="xs:integer">
<xs:minInclusive value="1"/>
<xs:maxExclusive value="100"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="AGE" type="ageType">
In the first example, the element “AGE” has a type defined by the unnamed simple type found within the definition of the element AGE itself (defined by the statement <xs:simpleType>).
In the second example however, the sample simple type has been given the name “ageType”. It now constitutes a complete named simple type definition that can be referenced anywhere. For instance it is used to type the element “AGE”, as shown in boldface in the example on the right.
When a particular type needs to be used by several elements, it makes sense to define it with a “named type”. However, if the type is to be used for only one particular element, it can simply be used as an “unnamed type” in situ.
In this chapter we were introduced to the concept of types and we focused on Simple Types. In the next chapter we will look at Complex Types – a very important and powerful way of declaring complex definitions using Schemas.