9.4 Facets
9.4.1 Bounds facets
The four bounds facets (minInclusive, maxInclusive, minExclusive, and maxExclusive) restrict a value to a specified range. Our previous examples apply minInclusive and maxInclusive to restrict the value space of DressSizeType. While minInclusive and max-Inclusive specify boundary values that are included in the valid range, minExclusive and maxExclusive specify values that are outside the valid range.
There are several constraints associated with the bounds facets:
minInclusiveand minExclusivecannot both be applied to the same type. Likewise, maxInclusive and maxExclusive cannot both be applied to the same type. You may, however, mix and match, applying minInclusiveand maxExclusive together. You may also apply just one end of the range, such as minInclusiveonly.
The value for the lower bound (minInclusiveor minExclu-sive) must be less than or equal to the value for the upper bound (maxInclusiveor maxExclusive).
The facet value must be a valid value for the base type. For example, when restricting integer, it is illegal to specify a maxInclusive value of 18.5, because 18.5 is not a valid integer.
The four bounds facets can be applied only to the date/time and numeric types, and types derived from them. Special consideration should be given to time zones when applying bounds facets to date and time types. For more information, see Section 12.4.12, "Date and time ordering."
9.4.2 Length facets
The length facet allows you to limit values to a specific length. If it is a string-based type, length is measured in number of characters. This includes the legacy types and anyURI. If it is a binary type, length is measured in octets of binary data. If it is a list type, length is measured in number of items in the list. The facet value for length must be a non-negative integer.
The minLength and maxLength facets allow you to limit a value's length to a specific range. Either of both of these facets may be applied. If they are both applied, minLength must be less than or equal to maxLength. If the length facet is applied, neither minLength nor maxLength may be applied. The facet values for minLength and maxLength must be non-negative integers.
The three length facets (length, minLength, maxLength) can be applied to any of the string-based types (including the legacy types), the binary types, QName, and anyURI. They cannot be applied to the date/time types, numeric types, or boolean.
9.4.2.1 Design hint: What if I want to allow empty values?
Many of the built-in types do not allow empty values. Types other than string, normalizedString, token, hexBinary, and base64Binary do not allow an empty value, unless xsi:nil appears in the element tag.
There may be a case where you have an integer that you want to be either between 2 and 18, or empty. First, consider whether you want to make the element (or attribute) optional. In this case, if the data is absent, the element will not appear at all. However, sometimes it is desirable for the element to appear, as a placeholder, or perhaps it is unavoidable because of the technology used to generate the instance.
If you do determine that the elements must be able to appear empty, you must define a union data type that includes both the integer type and an empty string. For example:
<xsd:simpleType name="DressSizeType"> <xsd:union> <xsd:simpleType> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="2"/> <xsd:maxInclusive value="18"/> </xsd:restriction> </xsd:simpleType> <xsd:simpleType> <xsd:restriction base="xsd:token"> <xsd:enumeration value=""/> </xsd:restriction> </xsd:simpleType> </xsd:union> </xsd:simpleType>
9.4.2.2 Design hint: What if I want to restrict the length of an integer?
The length facet only applies to the string-based types, the legacy types, the binary types, and anyURI. It does not make sense to try to limit the length of the date and time types because they have fixed lexical representations. But what if you want to restrict the length of an integer value?
You can restrict the lower and upper bounds of an integer by applying bounds facets, as discussed in Section 9.4.1, "Bounds facets." You can also control the number of significant digits in an integer using the totalDigits facet, as discussed in Section 9.4.3, "totalDigits and fractionDigits." However, these facets do not consider leading zeros to be significant. Therefore, they cannot force the integer to appear in the instance as a specific number of digits. To do this, you need a pattern. For example, the pattern \d{1,2} used in our Dress-SizeType example forces the size to be one or two digits long, so 012 would be invalid.
Before taking this approach, however, you should reconsider whether it is really an integer or a string. See Section 12.3.3.1, "Design hint: Is it an integer or a string?" for a discussion of this issue.
9.4.3 totalDigits and fractionDigits
The totalDigits facet allows you to specify the maximum number of digits in a number. The facet value for totalDigits must be a positive integer.
The fractionDigits facet allows you to specify the maximum number of digits in the fractional part of a number. The facet value for fractionDigits must be a non-negative integer, and it must not exceed the value for totalDigits, if one exists.
The totalDigits facet can be applied to decimal or any of the integer types, and types derived from them. The fractionDigits facet may only be applied to decimal, because it is fixed at 0 for all integer types.
9.4.4 Enumeration
The enumeration facet allows you to specify a distinct set of valid values for a type. Unlike most other facets (except pattern), the enumeration facet can appear multiple times in a single restriction. Each enumerated value must be unique, and must be valid for that type. If it is a string-based or binary data type, you may also specify the empty string in an enumeration value, which allows elements or attributes of that type to have empty values.
Example 98 shows a simple type SMLXSizeType that allows the values small, medium, large, and extra large.
When restricting types that have enumerations, it is important to note that you must restrict, rather than extend, the set of enumeration values. For example, if you want to restrict the valid values of SMLSize-Type to only be small, medium, and large, you could define a simple type as in Example 99.
Note that you need to repeat all of the enumeration values that apply to the new type. This example is legal because the values for SMLSize-Type (small, medium, and large) are a subset of the values for SMLXSizeType. By contrast, Example 910 attempts to add an enumeration facet to allow the value extra small. This type definition is illegal because it attempts to extend rather than restrict the value space of SMLXSizeType.
Example 98. Applying the enumeration facet
<xsd:simpleType name="SMLXSizeType"> <xsd:restriction base="xsd:token"> <xsd:enumeration value="small"/> <xsd:enumeration value="medium"/> <xsd:enumeration value="large"/> <xsd:enumeration value="extra large"/> </xsd:restriction> </xsd:simpleType>
Example 99. Restricting an enumeration
<xsd:simpleType name="SMLSizeType"> <xsd:restriction base="SMLXSizeType"> <xsd:enumeration value="small"/> <xsd:enumeration value="medium"/> <xsd:enumeration value="large"/> </xsd:restriction> </xsd:simpleType>
Example 910. Illegal attempt to extend an enumeration
<xsd:simpleType name="XSMLXSizeType"> <xsd:restriction base="SMLXSizeType"> <xsd:enumeration value="extra small"/> <xsd:enumeration value="small"/> <xsd:enumeration value="medium"/> <xsd:enumeration value="large"/> <xsd:enumeration value="extra large"/> </xsd:restriction> </xsd:simpleType>
The only way to add an enumeration value to a type is by defining a union type. Example 911 shows a union type that adds the value extra small to the set of valid values. Union types are described in detail in Chapter 11, "Union and list types."
Example 911. Using a union to extend an enumeration
<xsd:simpleType name="XSMLXSizeType"> <xsd:union memberTypes="SMLXSizeType"> <xsd:simpleType> <xsd:restriction base="xsd:token"> <xsd:enumeration value="extra small"/> </xsd:restriction> </xsd:simpleType> </xsd:union> </xsd:simpleType>
When enumerating numbers, it is important to note that the enumeration facet works on the actual value of the number, not its lexical representation as it appears in an XML instance. Example 912 shows a simple type NewSmallDressSizeType that is based on integer, and specifies an enumeration of 2, 4, and 6. The two instance elements shown, which contain 2 and 02, are both valid. This is because 02 is equivalent to 2 for integer-based types. However, if the base type of NewSmallDressSizeType had been string, the value 02 would not be valid, because the strings 2 and 02 are not the same. If you wish to constrain the lexical representation of a numeric type, you should apply the pattern facet instead. For more information on type equality in XML Schema, see Section 12.7, "Type equality." The enumeration facet can be applied to any type except boolean.
9.4.5 Pattern
The pattern facet allows you to restrict values to a particular pattern, represented by a regular expression. Chapter 10, "Regular expressions," provides more detail on the rules for the regular expression syntax. Unlike most other facets (except enumeration), the pattern facet can be specified multiple times in a single restriction. If multiple pattern facets are specified in the same restriction, the instance value must match at least one of the patterns. It is not required to match all of the patterns.
Example 912. Enumerating numeric values
Schema:
<xsd:simpleType name="NewSmallDressSizeType"> <xsd:restriction base="xsd:integer"> <xsd:enumeration value="2"/> <xsd:enumeration value="4"/> <xsd:enumeration value="6"/> </xsd:restriction> </xsd:simpleType>
Valid instances:
<size>2</size> <size>02</size>
Example 913 shows a simple type DressSizeType that includes the pattern \d{1,2}, which restricts the size to one or two digits.
Example 913. Applying the pattern facet
<xsd:simpleType name="DressSizeType"> <xsd:restriction base="xsd:integer"> <xsd:minInclusive value="2"/> <xsd:maxInclusive value="18"/> <xsd:pattern value="\d{1,2}"/> </xsd:restriction> </xsd:simpleType>
When restricting types that have patterns, it is important to note that you must restrict, rather than extend, the set of valid values that the patterns represent. In Example 914, we define a simple type SmallDressSizeType that is derived from DressSizeType, and add an additional pattern facet that restricts the size to one digit.
Example 914. Restricting a pattern
<xsd:simpleType name="SmallDressSizeType"> <xsd:restriction base="DressSizeType"> <xsd:minInclusive value="2"/> <xsd:maxInclusive value="6"/> <xsd:pattern value="\d{1}"/> </xsd:restriction> </xsd:simpleType>
It is not technically an error to apply a pattern facet that does not represent a subset of the ancestors' pattern facets. However, the schema processor tries to match the instance value against the pattern facet of both the type and its ancestors, ensuring that it is in fact a subset. Example 915 shows an illegal attempt to define a new size type that allows the size value to be up to three digits long. While the schema is not in error, it will not have the desired effect because the schema processor will check values against both the pattern of LongerDress-SizeType and the pattern of DressSizeType. The value 004 would not be considered a valid instance of LongerDressSizeType because it does not conform to the pattern of DressSizeType.
Unlike the enumeration facet, the pattern facet applies to the lexical representation of the value. If the value 02 appears in an instance, the pattern is applied to the digits 02, not 2 or +2 or any other form of the integer.
The pattern facet can be applied to any type.
Example 915. Illegal attempt to extend a pattern
<xsd:simpleType name="LongerDressSizeType"> <xsd:restriction base="DressSizeType"> <xsd:pattern value="\d{1,3}"/> </xsd:restriction> </xsd:simpleType>
9.4.6 Whitespace
The whiteSpace facet allows you to specify the whitespace normalization rules which apply to this value. Unlike the other facets, which restrict the value space of the type, the whiteSpace facet is an instruction to the schema processor as to what to do with whitespace. The valid values for the whiteSpace facet are:
preserve: All whitespace is preserved; the value is not changed. This is how XML 1.0 processors handle whitespace in the character data content of elements.
replace: Each occurrence of a tab (#x9), line feed (#xA), and carriage return (#xD) is replaced with a single space (#x20). This is how XML 1.0 processors handle whitespace in attributes of type CDATA.
collapse: As with replace, each occurrence of tab (#x9), line feed (#xA) and carriage return (#xD) is replaced with a single space (#x20). After the replacement, all consecutive spaces are collapsed into a single space. In addition, leading and trailing spaces are deleted. This is how XML 1.0 processors handle whitespace in all attributes that are not of type CDATA.
Table 96 shows examples of how values of a string-based type will be handled depending on its whiteSpace facet.
Table 96 Handling of string values depending on whiteSpace facet
Original string |
string |
normalizedString |
token |
|
(preserve) |
(replace) |
(collapse) |
a string |
a string |
a string |
a string |
on |
on |
on two lines |
on two lines |
two lines |
two lines |
|
|
has spaces |
has spaces |
has spaces |
has spaces |
leading tab |
leading tab |
leading tab |
leading tab |
leading spaces |
leading spaces |
leading spaces |
leading spaces |
The whitespace processing, if any, will happen first, before any validation takes place. In Example 98, the base type of SMLXSizeType is token, which has a whiteSpace facet of collapse. Example 916 shows valid instances of SMLXSizeType. They are valid because the leading and trailing spaces are removed, and the line feed is turned into a space. If the base type of SMLXSizeType had been string, the whitespace would have been left as is, and these values would have been invalid.
Example 916. Valid instances of SMLXSizeType
<size> small </size> <size>extra large</size>
Although you should understand what the whiteSpace facet represents, it is unlikely that you will ever apply it directly in your schemas. The whiteSpace facet is fixed at collapse for most built-in types. Only the string-based types can be restricted by a whiteSpace facet, but this is not recommended. Instead, select a base type that already has the whiteSpace facet you want. The data types string, normalizedString, and token have the whiteSpace values preserve, replace, and collapse, respectively. For example, if you wish to define a string-based type that will have its whitespace collapsed, base your type on token, instead of basing your type on string and applying a whiteSpace facet. Section 12.2.1, "string, normalized-String, and token," provides a discussion of these three types.