6.7. Simple Types

A simple type specifies a value range. An element type or attribute type may specify a simple type to impose a range of values on a corresponding XML instance. In the simplest case, a built-in datatype imposes the value range. Frequently, a simple type derived from a built-in datatype provides further constraints. A constraining facet on the derived simple type specifies a restriction of the value range. Multiple constraining facets may apply to a single simple type.

6.7.1. Built-in Datatypes

The built-in datatypes fall mostly into three categories: strings, numbers, and dates. Each of these categories has multiple specific datatypes. For example, a date might or might not include the time. Alternatively, perhaps the only interesting part of a date is the year. Similarly, numbers can be broken into integer and floating point, positive and negative, and other subcategories. An element type, attribute type, simple type, or complex type may reference a built-in datatype. A trivial example is the XML representation of basicTokenElement, whose structure type is the built-in token datatype described in Section 6.5.1.1:

<xsd:element name="basicTokenElement" type="xsd:token"/> 

Similarly, the decimalAttribute attribute type specifies a decimal number:

<xsd:attribute name="decimalAttribute" type="xsd:decimal"/> 

Chapter 12 discusses all built-in datatypes.

6.7.2. Deriving Simple Types by Restriction

A built-in datatype does not always provide appropriate constraints. A custom simple type creates an appropriate constraint by restricting a built-in datatype or another custom simple type. The XML representation of the element type sixToOneHundred extracted from Listing 6.1 specifies a simple type that constrains the built-in datatype positiveInteger to a number between 6 and 100:

<xsd:element name="sixToOneHundred" 
             minOccurs="0" 
             maxOccurs="unbounded"> 
    <xsd:simpleType> 
        <xsd:restriction base="xsd:positiveInteger"> 
            <xsd:minExclusive value="5"/> 
            <xsd:maxInclusive value="100"/> 
        </xsd:restriction> 
    </xsd:simpleType> 
</xsd:element> 

Note that the value range is exclusive of the value 5 and inclusive of the value 100.

6.7.3. Constraining Facets

A simple type applies zero or more constraining facets during derivation. The constraining facets limit the range of values, as demonstrated by the use of minExclusive and maxInclusive in the element type sixToOneHundred described in the previous example. Table 6.1 introduces all the constraining facets. This introduction includes a brief description of each constraining facet.

Table 6.1. The Constraining Facets
Element Description
enumeration The value of an enumeration constraining facet is a set of specific valid values for a corresponding element. Each member of the set is represented by a separate enumeration element.
fractionDigits The value of a fractionDigits constraining facet limits the number of digits after the decimal point required to represent a decimal value.
length The value of a length constraining facet specifies the length of a string in characters. The value of a length constraining facet may also specify the length of a list (that is, the number of items in the list). For other datatypes, the units (characters, items) may be something different, appropriate to the datatype.
maxExclusive The value of a maxExclusive constraining facet specifies an upper bound on a numeric value. This boundary excludes the value specified.
maxInclusive The value of a maxInclusive constraining facet specifies an upper bound on a numeric value. This boundary includes the value specified.
maxLength The value of a maxLength constraining facet specifies the maximum number of characters in a string or the maximum number of items in a list (or the maximum “something” appropriate to the datatype constrained).
minExclusive The value of a minExclusive constraining facet specifies a lower bound on a numeric value. This boundary excludes the value specified.
minInclusive The value of a minInclusive constraining facet specifies a lower bound on a numeric value. This boundary includes the value specified.
minLength The value of a minLength constraining facet specifies the minimum number of characters in a string or the minimum number of items in a list.
pattern The value of a pattern constraining facet is a regular expression used to validate a character string.
totalDigits The value of a totalDigits constraining facet limits the total number of digits required to represent a decimal value.
whiteSpace The value of a whiteSpace constraining facet provides for various kinds of normalization of spaces, carriage returns, and line feeds when determining the value of an instance of a simple type.

Each constraining facet applies to a subset of the datatypes. Chapter 12 discusses all the constraining facets.

6.7.4. Lists

A simple type may specify a list of values. A simple type list derives from yet another simple type that constrains each value in the list. The values in a list are space-delimited. The thematic catalog example specifies a list of part numbers. An XML instance of this part number list might look like the following:

<partList>UX002 UX003</partList> 

Because of the complexity of creating a list, discussion about creating the XML representation of a list simple type is left to Section 10.7.

6.7.5. Unions

A simple type may specify a value space and lexical space in terms of a union of other simple types. A simple type may form a union from fundamentally different simple types. The thematic catalog example portrays the simple type assemblyPartStatusType that specifies a union of a date, an integer, and a string. A corresponding XML instance must be an instance of one of these three valid value classes.

Because of the complexity of creating a union, discussion about creating the XML representation of a union simple type is left to Section 10.8.

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

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