XML technologies / XML Schema / Creating simple data types / Derivation by restriction
(Excerpt from “XML Schema” by Eric van der Vlist)
Restriction is probably the most common and natural derivation method. Data types are created by restriction, adding constraints to permitted values. W3C XML Schema itself uses restriction for most predefined derived types, such as xs:positiveInteger, derived by restriction from xs:integer . Restrictions can be defined along different aspects or axes, which W3C XML Schema calls “facets”.
Restriction uses the element xs:restriction, defining each facet with a separate element embedded in xs:restriction. The restricted type is the base data type, referenced through attribute base or defined inside element xs:restriction :
<xs:simpleType name="myInteger">
<xs:restriction base="xs:integer">
<xs:minInclusive value="-2"/>
<xs:maxExclusive value="5"/>
</xs:restriction>
</xs:simpleType>Derivation can also take two steps using an embedded definition of xs:simpleType :
<xs:simpleType name="myInteger">
<xs:restriction>
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:maxExclusive value="5"/>
</xs:restriction>
</xs:simpleType>
<xs:minInclusive value="-2"/>
</xs:restriction>
</xs:simpleType>The elements xs:minInclusive and xs:maxExclusive are two facets for integer types, specifying an inclusive minimum, accepted, and exclusive maximum, not accepted. The next section lists the facets. Each operates directly on the value space or on the lexical space, depending on the facet; the same facet can affect different types differently.
Whatever facet is applied, the base type’s semantics remain unchanged. Available facets cannot be extended, so choose a base type matching the node’s purpose whenever possible. For example, patterns can restrict strings to non-ISO dates, but use this only when unavoidable: the result remains a string, lacking date facets such as xs:minInclusive or xs:maxExclusive, meaningless for strings within W3C XML Schema.
Choosing a base type whose semantics closely match actual instance use will become more critical as applications supporting W3C XML Schema become available. Their behavior will depend on type information in the PSVI. A poor choice causes side effects. Early drafts of XPath 2.0, for example, propose interpreting values by predefined type, making equality tests and sort order type-dependent.
Facets
Before listing facets, let us consider their operation in three categories: xs:whitespace specifies whitespace processing between parsed and lexical spaces, but applies only to xs:string and xs:normalizedString. xs:pattern operates on the lexical space; all other facets restrict the value space. Availability and effects depend on the type. We examine groups sharing the same facets.
Strings with collapsed whitespace
These are all character strings, although W3C XML Schema does not technically consider them all derived from xs:string, with whitespace collapsed before validation: every #x9, tab, #xA, linefeed, and #xD, carriage return, becomes #x20, space; consecutive #x20 characters collapse to one and leading or trailing #x20 characters are removed.
These types are: xs:ENTITY, xs:ID, xs:IDREF, xs:language, xs:Name, xs:NCName, xs:NMTOKEN, xs:token, Einschränkungen, xs:base64Binary, xs:hexBinary, xs:NOTATION and xs:QName. The next section explains their facets.
xs:enumeration
xs:enumeration specifies a list of possible values, for example:
<xs:simpleType name="schemaRecommendations">
<xs:restriction base="xs:anyURI">
<xs:enumeration value="http://www.w3.org/TR/xmlschema-0/"/>
<xs:enumeration value="http://www.w3.org/TR/xmlschema-1/"/>
<xs:enumeration value="http://www.w3.org/TR/xmlschema-2/"/>
</xs:restriction>
</xs:simpleType>This restricts the value space. Lexical and value spaces coincide for most string-like types, but differ for Einschränkungen, xs:base64Binary and xs:QName. For example, "http://dmoz.org/World/Français/" and "http://dmoz.org/World/Fran%c3%a7ais/" are identical for Einschränkungen; line breaks are ignored for xs:base64Binary; and for xs:QName the check compares tuples {namespace URI, local name}, ignoring the prefix used in the schema and instance.
Note also that types Einschränkungen are not made absolute by W3C XML Schema and do not support xml:base. Thus the previous type schemaRecommendations, assigned to an attribute href in the sense of XLink, will not validate this instance element:
<a xml:base="http://www.w3.org/TR/" href="xmlschema-1/">
XML Schema Teil 2: Datentypen
</a>
We cannot end without discussing xs:NOTATION. This is the only predefined type unavailable directly in a schema; it requires derived types specifying a set of facets xs:enumeration. Notations are rare in real applications, but at least one example is needed. Consider the usual image example, with an attribute notation describing a binary field’s content:
<?xml version="1.0"?>
<picture type="png">
iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAABmJLR0QA/wD/AP+gvaeTAAAA
CXBIWXMAAAsSAAALEgHS3X78AAAAB3RJTUUH0QofESYx2JhwGwAAAFZJREFUeNqlj8ENwDAI
A6HqGDCWp2QQ2AP2oI9IbaQm/dRPn9EJ7m7a56DPPDgiIoKIzGyBM9Pdx+4ueXabWVUBEJHR
nLNJVbfuqspMAEOxwO9r/vX3BTEnKRXtqqslAAAAAElFTkSuQmCC
</picture>
Its schema follows. Note that notations must be declared before use in a facet xs:enumeration:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:notation name="jpeg" public="image/jpeg"
system="file:///usr/bin/xv"/>
<xs:notation name="gif" public="image/gif"
system="file:///usr/bin/xv"/>
<xs:notation name="png" public="image/png"
system="file:///usr/bin/xv"/>
<xs:notation name="svg" public="image/svg"
system="file:///usr/bin/xsmiles"/>
<xs:notation name="pdf" public="application/pdf"
system="file:///usr/bin/acroread"/>
<xs:simpleType name="graphicalFormat">
<xs:restriction base="xs:NOTATION">
<xs:enumeration value="jpeg"/>
<xs:enumeration value="gif"/>
<xs:enumeration value="png"/>
<xs:enumeration value="svg"/>
<xs:enumeration value="pdf"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="picture">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:base64Binary">
<xs:attribute name="type" type="graphicalFormat"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:schema>xs:length
xs:length defines a fixed length in characters generally, or bytes for xs:hexBinary and xs:base64Binary:
<xs:simpleType name="standardNotations">
<xs:restriction base="xs:NOTATION">
<xs:length value="8"/>
</xs:restriction>
</xs:simpleType>This also restricts the value space. For Einschränkungen, length is hard to predict because it follows character normalization. It is harder for xs:QName, since the Recommendation for W3C XML Schema did not define the length of a tuple xs:QName. Fortunately these length restrictions seem of little use, so avoid them. The same applies to the following two facets.
xs:maxLength
xs:maxLength defines maximum length in characters generally, or bytes for xs:hexBinary and xs:base64Binary:
<xs:simpleType name="binaryImage">
<xs:restriction base="xs:hexBinary">
<xs:maxLength value="1024"/>
</xs:restriction>
</xs:simpleType>xs:minLength
xs:minLength defines minimum length in characters generally, or bytes for hexBinary and base64Binary:
<xs:simpleType name="longName">
<xs:restriction base="xs:NCName">
<xs:minLength value="6"/>
</xs:restriction>
</xs:simpleType>xs:pattern
xs:pattern defines a pattern that the string must match.
<xs:simpleType name="httpURI">
<xs:restriction base="xs:anyURI">
<xs:pattern value="http://.*"/>
</xs:restriction>
</xs:simpleType>Several pattern facets in one derivation step are combined by logical OR: matching any one is enough.
No single ordering suits every regional alphabet, so W3C XML Schema treats strings as unordered, with no facets for minimum or maximum string values.
Other strings
These strings do not collapse whitespace before validation. An additional facet, xs:whitespace, defines whitespace processing for custom types derived from them.
These types are: xs:normalizedString and xs:string.
xs:whitespace
xs:whitespace specifies processing of #x20, space, #x9, tab, #xA, linefeed, and #xD, carriage return, for the type:
<xs:simpleType name="CapitalizedNameWS">
<xs:restriction base="xs:string">
<xs:whiteSpace value="collapse"/>
<xs:pattern value="([A-Z]([a-z]*) ?)+"/>
</xs:restriction>
</xs:simpleType>The facet xs:whitespace accepts “preserve”, leaving whitespace unchanged; “replace”, replacing each whitespace character with a space; and “collapse”, removing leading/trailing whitespace and replacing other runs with one space. Unlike most facets, it specifies preprocessing before validation. In the example, collapse permits checking for a single space, “?”, in the pattern: whitespace collapses before pattern matching, so arbitrarily many spaces are covered.
Restriction cannot relax whitespace processing: preserve allows any behavior in derived types, replace allows only replace or collapse, and collapse requires collapse throughout. Thus xs:string alone supports deriving types with no whitespace processing, while xs:string and xs:normalizedString alone support deriving types that normalize whitespace.
In practice, this facet has little use for custom types because whitespace processing largely determines the predefined base type. No processing requires xs:string rather than xs:whitespace. To normalize whitespace, use xs:normalizedString directly instead of xs:string with the facet xs:whitespace, which has the same effect. For collapsed whitespace, use xs:token for strings; as noted, xs:token is a tokenized string rather than a token in the usual sense. Any non-string type also has collapse set already, without using xs:whitespace. The example is therefore equivalent to:
<xs:simpleType name="CapitalizedNameWS">
<xs:restriction base="xs:token">
<xs:pattern value="([A-Z]([a-z]*) ?)+"/>
</xs:restriction>
</xs:simpleType>Technically, the Working Group made facet xs:whitespace for xs:token and its derived types nonfixed. However, the value of xs:whitespace for xs:token is collapse. Since derivations cannot relax it, no derived type can change it.
Floating-point data types
The facets for xs:double and xs:float are described next.
xs:enumeration
xs:enumeration lists possible values in the value space, for example:
<xs:simpleType name="enumeration">
<xs:restriction base="xs:float">
<xs:enumeration value="-INF"/>
<xs:enumeration value="1.570796327"/>
<xs:enumeration value="3e3"/>
</xs:restriction>
</xs:simpleType>This simple type matches constants such as:
<enumeration>
1.570796327
</enumeration>
<enumeration>
3e3
</enumeration>
<enumeration>
003000.0000
</enumeration>
As briefly seen for Einschränkungen, xs:QName and xs:base64Binary, two lexical forms, “3e3” and “003000.0000”, represent the same value. Both are accepted by the same enumerated value.
xs:maxExclusive
xs:maxExclusive sets an exclusive maximum:
<xs:simpleType name="maxExclusive">
<xs:restriction base="xs:float">
<xs:maxExclusive value="10"/>
</xs:restriction>
</xs:simpleType>This validates “9.999999999999999” but not “10”.
The facet xs:maxExclusive is particularly useful for types such as xs:float , xs:double, xs:decimal and even date/time values handling infinitesimal values, where no greatest value below a given value can be determined.
xs:maxInclusive
xs:maxInclusive sets an inclusive maximum:
<xs:simpleType name="thousands">
<xs:restriction base="xs:double">
<xs:maxInclusive value="1e3"/>
</xs:restriction>
</xs:simpleType>xs:minExclusive
xs:minExclusive sets an exclusive minimum:
<xs:simpleType name="strictlyPositive">
<xs:restriction base="xs:double">
<xs:minExclusive value="0"/>
</xs:restriction>
</xs:simpleType>xs:minInclusive
xs:minInclusive sets an inclusive minimum:
<xs:simpleType name="positive">
<xs:restriction base="xs:double">
<xs:minInclusive value="0"/>
</xs:restriction>
</xs:simpleType>xs:pattern
xs:pattern defines a pattern matching the type’s lexical value:
<xs:simpleType name="nonScientific">
<xs:restriction base="xs:float">
<xs:pattern value="[^eE]*"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="noLeading0">
<xs:restriction base="xs:float">
<xs:pattern value="[^0].*"/>
</xs:restriction>
</xs:simpleType>A pattern on the floating-point lexical value can prohibit scientific notation, xxxEyyy, or leading zeros.
xs:pattern is the only facet operating directly on the lexical space.
Date and time data types
These partially ordered types allow bounds with some limitations. They are: xs:date, xs:dateTime, xs:duration, xs:gDay, xs:gMonth, xs:gMonthDay, xs:gYear, xs:gYearMonth and xs:time. Their facets are the same as for floating-point types:
xs:enumeration
xs:enumeration lists possible values in the value space, for example:
<xs:simpleType name="ModernSwissHistoricalDates">
<xs:restriction base="xs:gYear">
<xs:enumeration value="1864"/>
<xs:enumeration value="1872"/>
<xs:enumeration value="1914"/>
<xs:enumeration value="1939"/>
<xs:enumeration value="1971"/>
<xs:enumeration value="1979"/>
<xs:enumeration value="1992"/>
</xs:restriction>
</xs:simpleType>This simple type matches constants such as:
1939
Without a zone in the enumeration, the zone is unknown; these dates match no date with a specified zone:
1939Z
or:
1939+10:00
The same problem occurs with zoned enumerations, such as:
<xs:simpleType name="wakeUpTime">
<xs:restriction base="xs:time">
<xs:enumeration value="07:00:00-07:00"/>
<xs:enumeration value="07:15:00-07:00"/>
<xs:enumeration value="07:30:00-07:00"/>
<xs:enumeration value="07:45:00-07:00"/>
<xs:enumeration value="08:00:00-07:00"/>
</xs:restriction>
</xs:simpleType>This new type matches:
07:00:00-07:00
as well as:
11:00:00-04:00
and even:
07:15:00-07:15
But it validates no time without a zone.
Handling both zoned and zoneless times may be questionable, but enumeration can mix them:
<xs:simpleType name="sevenOClockPST">
<xs:restriction base="xs:time">
<xs:enumeration value="07:00:00-07:00"/>
<xs:enumeration value="07:00:00"/>
</xs:restriction>
</xs:simpleType>xs:maxExclusive
xs:maxExclusive sets an exclusive maximum:
<xs:simpleType name="beforeY2K">
<xs:restriction base="xs:dateTime">
<xs:maxExclusive value="2000-01-01T00:00:00Z"/>
</xs:restriction>
</xs:simpleType>This validates times before Y2K UTC, such as:
1999-12-31T23:59:59Z
or:
1999-12-31T23:59:59.999999999999Z
It also validates the following, even expressed in another zone:
2000-01-01T11:59:59+12:00
But not:
2000-01-01T00:00:00Z
Comparison with a zoneless date/time applies a +/-14-hour uncertainty interval. The greatest allowed zoneless value here, excluding fractional seconds, is:
1999-12-31T09:59:59
xs:maxInclusive
xs:maxInclusive sets an inclusive maximum:
<xs:simpleType name="AQuarterOrLess">
<xs:restriction base="xs:duration">
<xs:maxInclusive value="P3M"/>
</xs:restriction>
</xs:simpleType>This accepts durations of at most three months. P2M, two months, and P3M, three months, pass. Mixing months and days, P2M30D, two months and 30 days, passes, but P2M31D, two months and 31 days, and P2M30DT1S, two months, 30 days and one second, fail because combining years/months with days/hours/minutes/seconds makes the actual duration uncertain.
xs:minExclusive
xs:minExclusive sets an exclusive minimum:
<xs:simpleType name="afterTeaTimeInParisInSummer">
<xs:restriction base="xs:time">
<xs:minExclusive value="17:00:00+02:00"/>
</xs:restriction>
</xs:simpleType>xs:minInclusive
xs:minInclusive sets an inclusive minimum:
<xs:simpleType name="afterOrOnThe20th">
<xs:restriction base="xs:gDay">
<xs:minInclusive value="---20"/>
</xs:restriction>
</xs:simpleType>Returning to durations, we can define:
<xs:simpleType name="AQuarterOrMore">
<xs:restriction base="xs:duration">
<xs:minInclusive value="P3M"/>
</xs:restriction>
</xs:simpleType>This accepts at least three months. P4M, four months, and P3M, three months, pass. With months and days, P2M31D, two months and 31 days, passes, but P2M30D, two months and 30 days, and P2M30DT23H59M59S, two months, 30 days, 23 hours, 59 minutes and 59 seconds, fail because the actual duration is uncertain.
This uncertainty makes W3C XML Schema treat our third month as 30 days with xs:minInclusive but 31 with xs:maxInclusive. In practice, prohibiting combinations causing uncertainty may be wise. The next chapter shows how with a pattern.
xs:pattern
xs:pattern defines a pattern on the lexical value. The next chapter details patterns; as an introduction, this type prohibits zones in values of xs:dateTime:
<xs:simpleType name="noTimeZone">
<xs:restriction base="xs:dateTime">
<xs:pattern value=".*T[^Z+-]*"/>
</xs:restriction>
</xs:simpleType>Integer and derived types
These types are: Beschreibung, xs:int, xs:integer, xs:long, xs:negativeInteger, xs:nonNegativeInteger, xs:nonPositiveInteger, xs:positiveInteger, xs:short, xs:unsignedByte, xs:unsignedInt, xs:unsignedLong and xs:unsignedShort.
They accept the same facets as floating-point and date/time types, plus a digit-count facet.
xs:totalDigits
xs:totalDigits sets the maximum decimal digit count:
<xs:simpleType name="totalDigits">
<xs:restriction base="xs:integer">
<xs:totalDigits value="5"/>
</xs:restriction>
</xs:simpleType>This accepts integers with at most five decimal digits.
xs:totalDigits operates on the value space: “000012345”, canonical value “12345”, matches this type.
Decimals
The simple type xs:decimal accepts all integer facets plus one limiting fractional digits.
xs:fractionDigits
xs:fractionDigits sets the maximum number of digits after the decimal point:
<xs:simpleType name="fractionDigits">
<xs:restriction base="xs:decimal">
<xs:fractionDigits value="2"/>
</xs:restriction>
</xs:simpleType>This accepts decimals with at most two fractional digits.
xs:fractionDigits operates on the value space: “1.12000”, canonical value “1.12”, matches this type.
Boolean type
With only one permitted facet, xs:boolean is simplest in restriction facets. Its value space is true and false, but its lexical space also contains 0 and 1. The facet xs:pattern can exclude one format.
xs:pattern
The functionality of xs:pattern is normally extensive, but the few possible values of xs:boolean seem to leave only fixing a format as its use here:
<xs:simpleType name="trueOrFalse">
<xs:restriction base="xs:boolean">
<xs:pattern value="true"/>
<xs:pattern value="false"/>
</xs:restriction>
</xs:simpleType>List data types
Facets available for list types, xs:IDREFS, xs:ENTITIES and xs:NMTOKENS, are those of list-derived types, discussed next.
Multiple restrictions and fixed attributes
New restrictions can apply to types already derived by restriction.
Facets not previously restricted are added to the existing set. The new lexical and value spaces intersect all constraints. Redefining the same facets is more complicated: restriction facets can then expand the value space.
Repeated definitions fall into four categories:
Facets that may change only by becoming stricter
This is the most common case. xs:enumeration, xs:fractionDigits, xs:maxExclusive, Beschreibung, xs:maxLength, xs:minExclusive, xs:minInclusive, xs:minLength and xs:totalDigits belong here.
None may be added in a way that expands the base type’s value space. Examples of errors:
<xs:simpleType name="minInclusive">
<xs:restriction base="xs:float">
<xs:minInclusive value="10"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="minInclusive2">
<xs:restriction base="minInclusive">
<xs:minInclusive value="0"/>
</xs:restriction>
</xs:simpleType>
or:
<xs:simpleType name="enumeration">
<xs:restriction base="xs:float">
<xs:enumeration value="-INF"/>
<xs:enumeration value="1.618033989"/>
<xs:enumeration value="3e3"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="enumeration2">
<xs:restriction base="enumeration">
<xs:enumeration value="0"/>
</xs:restriction>
</xs:simpleType>Facets that cannot change
The facet Facettexs:length is alone here. A derived type’s length cannot be redefined once its parent’s is fixed.
xs:length is shorthand for giving facets xs:maxLength and xs:minLength the same value. The behavior matches using both together: new values for xs:maxLength must be at most the length; new values for xs:minLength must be at least the length. Since xs:minLength must also be at most xs:maxLength, all must retain the previously defined length.
Facets intersecting lexical spaces
The facet xs:pattern alone can be applied repeatedly, always restricting lexical space by direct intersection. The following type nonScientificNoLeading0 must match both the base pattern and the new restriction:
<xs:simpleType name="nonScientific">
<xs:restriction base="xs:float">
<xs:pattern value="[^eE]*"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="nonScientificNoLeading0">
<xs:restriction base="nonScientific">
<xs:pattern value="[^0].*"/>
</xs:restriction>
</xs:simpleType>Facets acting before the lexical space
xs:whitespace is a notable exception: by specifying whitespace processing it can expand accepted instances during restriction, as shown:
<xs:simpleType name="greetings">
<xs:restriction base="xs:string">
<xs:whiteSpace value="replace"/>
<xs:enumeration value="hi"/>
<xs:enumeration value="hallo"/>
<xs:enumeration value="wie geht’s?"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="restricted-greetings">
<xs:restriction base="greetings">
<xs:whiteSpace value="collapse"/>
</xs:restriction>
</xs:simpleType>The first type, greetings, accepts:
wie geht’s?
But rejects:
wie geht’s?
The restricted type accepts both strings.
Fixed facets
Every facet except xs:enumeration and xs:pattern allows an attribute named fixed. If its value is true, later restriction cannot change the facet.
To prevent changing the minimum set by minInclusive, write:
<xs:simpleType name="minInclusive">
<xs:restriction base="xs:float">
<xs:minInclusive value="10" fixed="true"/>
</xs:restriction>
</xs:simpleType>The schema for W3C XML Schema uses this to fix facets for predefined derivations. For example, the type xs:integer derives from xs:decimal as follows:
<xs:simpleType name="integer" id="integer">
<xs:restriction base="xs:decimal">
<xs:fractionDigits value="0" fixed="true"/>
</xs:restriction>
</xs:simpleType>
<xs:enumeration> and <xs:pattern> cannot be fixed.
Copyright © 2003 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XML Schema” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de