C

XML Schema Data Types

This appendix shows the data types available for use in W3C XML Schemas, which were covered in Chapter 5.

THE TREE OF TYPES

The types in XML Schema follow the traditional pattern of a tree found in many other software libraries. For example, in Java the basic type is Object from which all other classes ultimately derive. The inheritance tree for the XML Schema types is shown in Figure C-1.

As you can see, all W3C Schema types are based on anyType. From here come all the built-in types, or you can create a complex type using restriction and extension, as detailed in Chapter 5.

These types are all grouped under the namespace http://www.w3.org/2001/XMLSchema. It is common to use a prefix of xs or xsd to represent this namespace URI but, because all the types discussed in this appendix fall under this namespace, no prefix is used when specifying them.

Most of the types shown can be constrained, that is they can be limited to hold a narrower range of data than originally intended. This is because the types have a number of facets. Each facet allows you to specify a property of the type. For example, the double type has a facet called minInclusive. This means you can specify the minimum value that can be held by this type. If you specified 0 for this property, your type would not be able to contain any negative values. Another common facet is pattern. This allows you to specify a regular expression that the lexical representation (that is, how it’s written) must follow. So you could start with a string type and specify a pattern of [A-Z] to restrict it to uppercase letters from the traditional Latin alphabet.

STYLES OF TYPES

The built-in types can have three different styles. They can be atomic meaning they cannot be broken down into a simpler type, for example, double. They can also be lists, in which there are many values of one type. For instance, NMTOKENS is a list of NMTOKEN. Lastly, they can be unions, in which a single item can be of one or more of the simple types. For an example of a union type, take the maxOccurs attribute on the <xs:element> element in XML Schema. This is used to specify how many times the element may appear at most. It can be either a positive integer, such as 3, or the string unbounded. As such it is a union of two types, positive integer and string.

THE BUILT-IN TYPES

Table C-1 gives a summary of most of the built-in types along with any facets that they might have and an example usage.

TABLE C-1: The Primitive Built-In XML Schema Types

image

image

image

image

Table C-2 shows the built-in data types that are derived from the primitive types in Table C-1.

TABLE C-2: The Derived Built-In XML Schema Types

image

image

image

image

image

image

FACETS

Table C-3 describes how each facet can be used to constrain a type.

TABLE C-3: XML Schema Type Facets

FACET NAME DESCRIPTION EXAMPLE
length The exact number of characters, bytes, or items in a list. <restriction base=”string”><length value=”6” /></restriction> means a string of exactly six characters.
minLength The minimum number of characters, bytes, or items in a list. <restriction base=”anyURI”><minLength value=”20” /></restriction> means http://www.wrox.com would fail but http://www.wrox.com/ would pass.
maxLength The maximum number of characters, bytes, or items in a list. <restriction base=”anyURI”><maxLength value=”19” /></restriction> means http://www.wrox.com/ would fail but http://www.wrox.com would pass.
pattern A regular expression that limits what the value can hold. <restiction base=”string”><pattern value=”[0-9]{5}(-[0-9]{4})?” /></restriction> limits a string to a representation of a U.S. postal code (ZIP). The expression means five digits followed by an optional group that starts with a hyphen and is followed by four digits.
enumeration Restricts the value by specifying a limited number of specific values. <restriction base=”gMonthDay”>
<enumeration value=”--07—04”/><enumeration value=”--12-25”/></restriction>
limits a gMonthDay to one of July 4th or December 25th.
whiteSpace How whitespace is treated. This can take one of three values. preserve: whitespace is left alone.
replace: All instances of whitespace such as tab (#x9), linefeed (#xA), and carriage return (#xD) are replaced with a space (#x20).
collapse: Like replace but then multiple contiguous spaces are replaced by a single space and any leading and trailing spaces are removed.
preserve
replace
collapse
maxExclusive The exclusive upper bound of a numeric data type. <restriction base=”integer”>
<maxExclusive value=”101” />
</restriction>
restricts an integer to 100 or less.
maxInclusive The inclusive upper bound of a numeric data type. <restriction base=”integer”>
<maxInclusive value=”100” />
</restriction>
restricts an integer to 100 or less.
minExclusive The exclusive lower bound of a numeric data type. <restriction base=”integer”>
<minExclusive value=”0” />
</restriction>
restricts an integer to 1 or more.
minInclusive The inclusive lower bound of a numeric data type. <restriction base=”integer”>
<minInclusive value=”1” />
</restriction>
restricts an integer to 1 or more.
totalDigits A positive integer specifying the maximum number of total digits used to represent a data type derived from decimal. <restriction base=”integer”>
<totalDigits value=”6” />
</restriction>
restricts an integer to between –999999 and 999999.
fractionDigits A positive integer specifying the maximum number of digits that can appear after the decimal point in a type derived from decimal. <restriction base=”decimal”>
<fractionDigits value=”2” />
</restriction>
means that 1.23 is allowed but not 1.234.

For the original specifications on XML Schema data types, which contain more information and where the definitions are more rigorous, see http://www.w3.org/TR/xmlschema-1/ and http://www.w3.org/TR/xmlschema-2/.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.34.39