Simple and Complex Datatypes
XML Schema Part 1 and 2 goes over the basic concepts of what datatypes can be declared in an XML schema and the properties associated with such datatypes. Simple built-in datatypes exist, along with complex datatypes defined by the XML schema designer. An example of a complex type definition for an address in a purchase order follows:<xsd:complexType name="USAddress">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="street" type="xsd:string"/>
<xsd:element name="city" type="xsd:string"/>
<xsd:element name="state" type="xsd:string"/>
<xsd:element name="zip" type="xsd:decimal"/>
</xsd:sequence>
<xsd:attribute name="country" type="xsd:NMTOKEN" fixed="US"/>
</xsd:complexType>
Important things to note are that XML Schema namespace prefixes appear even on the built-in datatypes, such as string, and that a complex data type is surrounded by an inner sequence tag. In addition, constraints such as minOccurs and maxOccurs, whose default values equal 1, could have been put on the name element for number of occurrences, as in the following:
<xsd:element name="name" type="xsd:string" minOccurs="1" maxOccurs="2"/>
XML Schema Part 2 lists all the possible simple built-in datatypes for XML Schema, as outlined in Table 4-1.
Simple Built-in Data Type | Example (Comments) |
---|---|
string | this is a string |
normalizedString | this is a string (newlines, tabs, carriage returns, etc., are translated into spaces) |
token | this is another string (newlines, tabs, carriage returns, etc., are translated into spaces; adjacent spaces are collapsed into 1 space; trailing and leading spaces are removed) |
byte | –1, 126 |
unsignedByte | 0, 126 |
base64Binary | GpM7 |
hexBinary | 0fff |
integer | –126789, 0, 126789 (integer values only) |
positiveInteger | 1, 2, 126789 (positive integer values only) |
negativeInteger | –126789, –2, –1 (negative integer values only) |
nonNegativeInteger | 0, 1, 126789 |
nonPositiveInteger | –126789, –1, 0 |
int | –1, 0, 2, 126789675 |
unsignedInt | 0, 1, 1267896754 |
long | –1, –2, 0, 12678967543233 |
unsignedLong | 0, 1, 3, 12678967543233 |
short | –1, –2, –5, 0, 1, 12678 |
unsignedShort | 0, 1, 5, 12678 |
decimal | –1.2, 0, 1.2, 10000.00 |
Float | –0, 0, 12, INF, NaN 1.0E-2 (32-bit floating point) |
Double | –0, 0, 13, INF, NaN 1.0E-20 (64-bit floating point) |
Boolean | true, false, 1, 0 |
Time | 21:21:21.000–01:00 (UTC) |
dateTime | 2001–01–01T121:21:21.000–01:00 (date + time zone + UTC) |
duration | P1Y2M3DT10H30M12.0S (year, month, day, hour, minute, second) |
date | 1999–05–31 |
gMonth | –01– |
gYear | 2001 |
gYearMonth | 2001–01 |
gDay | –31 |
gMonthDay | –05–31 |
Name | anyname (XML 1.0 Name) |
QName | xsd:anyname (XML Namespace Qualified Name) |
NCName | anyname (XML Namespace Qualified Name without the prefix and colon) |
anyURI | http://www.oracle.com |
language | en-US |
ID | (a unique token, XML 1.0 ID attribute) |
IDREF | (a token that matches an ID, XML 1.0 IDREF attribute) |
IDREFS | (list of IDREF, XML 1.0 IDREFS attribute) |
ENTITY | (XML 1.0 ENTITY attribute) |
ENTITIES | (XML 1.0 ENTITIES attribute) |
NOTATION | (XML 1.0 NOTATION attribute) |
NMTOKEN | US, Canada (XML 1.0 NMTOKEN attribute) |
NMTOKENS | US UK Canada (XML 1.0 NMTOKENS attribute) |
These simple datatypes can also be used as a base type for ones that you can create. You can create these user-defined types by specifying constraints in three ways. First you can define a type by restriction by specifying additional aspects such as a pattern, value range, etc. Secondly, you can define a list type made up of a set of simple datatypes. Finally, you can define a union type which can be satisfied from a set of types. There are, however, no extension constraints. Some additional examples are the string, normalizedString, token, base64Binary, hexBinary, Name, QName, NCName, anyURI, language, ID, IDREFS, ENTITY, ENTITIES, NOTATION, NMTOKEN, and NMTOKENS datatypes all can take the following facets: length, minLength, maxLength, pattern (this can be a regular expression such as a date format like MM/DD/YYYY), enumeration, and whiteSpace. The number-oriented datatypes, such as byte, unsignedByte, integer, positiveInteger, negativeInteger, nonNegativeInteger, nonPositiveInteger, int, unsignedInt, long, unsignedLong, short, unsignedShort, and decimal, all can take the following facets: maxInclusive, maxExclusive, minInclusive, minExclusive, totalDigits, and fractionDigits.
The following examples illustrate the syntax of restrictions on simple types:
<!-- Range -->
<xsd:simpleType>
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="0"/>
<xsd:maxInclusive value="100"/>
</xsd:restriction>
</xsd:simpleType>
<!-- Enumeration -->
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Audi"/>
<xsd:enumeration value="Golf"/>
<xsd:enumeration value="BMW"/>
</xsd:restriction>
</xsd:simpleType>
<!-- Patterns -->
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:pattern value="[a-zA-Z0-9]{8}"/>
</xsd:restriction>
</xsd:simpleType>
<!-- Whitespace: preserve, collapse, replace -->
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:whiteSpace value="preserve"/>
</xsd:restriction>
</xsd:simpleType>
<!-- String length -->
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:minLength value="5"/>
<xsd:maxLength value="8"/>
</xsd:restriction>
</xsd:simpleType>
Complex types also can be constrained, but they have the flexibility of accepting both restriction and extension constraints. The following examples illustrate the syntax for complex types along with their constraints:
<!-- Element Extensions -->
<xsd:complexType name="fpersont">
<xsd:complexContent>
<xsd:extension base="personinfo">
<xsd:sequence>
<xsd:element name="address" type="xsd:string"/>
<xsd:element name="city" type="xsd:string"/>
<xsd:element name="country" type="xsd:string"/>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
<!-- Attribute only -->>
<xsd:element name="product">
<xsd:complexType>
<xsd:attribute name="prodid" type="xsd:positiveInteger"/>
</xsd:complexType>
</xsd:element>
<!--Simple Content - Extension/Restriction -->
<xsd:element name="shoesize">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:integer">
<xsd:attribute name="country" type="xsd:string" />
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
<!-- Mixed Content -->
<xsd:element name="letter">
<xsd:complexType mixed="true">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="orderid" type="xsd:positiveInteger"/>
<xsd:element name="shipdate" type="xsd:date"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
XML Schema extends the DTD functionality of IDs and IDREFs by introducing user-defined KEYs and KEYREFs. The following examples illustrate their syntax:
<element name="purchaseReport">
<complexType>
<sequence>
<element name="regions" type="r:RegionsType">
<keyref name="dummy2" refer="r:pNumKey">
<selector xpath="r:zip/r:part"/>
<field xpath="@number"/>
</keyref>
</element>
<element name="parts" type="r:PartsType"/>
</sequence>
<attribute name="period" type="duration"/>
<attribute name="periodEnding" type="date"/>
</complexType>
<unique name="dummy1">
<selector xpath="r:regions/r:zip"/>
<field xpath="@code"/>
</unique>
<key name="r:pNumKey">
<selector xpath="r:parts/r:part"/>
<field xpath="@number"/>
</key>
</element>
Note that there is an XPath selector to specify the path to the named key as well as the attribute value to be used for the key’s value.
Note | The Oracle XML Database does not currently support KEY/KEYREFs directly; however, this functionality can be implemented using SQL constraints. |
Additionally, union types such as <xsd:union memberTypes=“mystates allstate”/>, complexTypes from simple types, mixed attributes for complexTypes to indicate data between child elements, anyType such as type=“xsd:anyType” to indicate that the element could be of any datatype, are also allowed. Annotations such as <xsd:annotation> are also allowed, because they are simply mechanisms to embed documentation in the schema, such as in this example:
<xsd:annotation>
<xsd:documentation xml:lang="en">
hi there
</xsd:documentation>
</xsd:annotation>
Finally, user-defined mechanisms such as an attribute group can be created to have a number of attributes associated with an element. This includes <xsd:attributeGroup name=“BookDelivery”> with a reference like <xsd:attributeGroup ref=“BookDelivery”> within the definition of the complexType. We recommend that you review the XML Schema Part 0:Primer for a full discussion of the specification. See the Appendix for the locations of all the XML Schema specifications.