8.4. Facets
8.4.1. Bounds facets
The four bounds facets (minInclusive, maxInclusive, minExclusive, and maxExclusive) restrict a value to a specified range. Our previous examples applied minInclusive and maxInclusive to restrict the value space of DressSizeType. While minInclusive and maxInclusive specify boundary values that are included in the valid range, minExclusive and maxExclusive specify bounds that are excluded from the valid range.
There are several constraints associated with the bounds facets:
- minInclusive and minExclusive cannot 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, for example, minInclusive and maxExclusive together. You may also apply just one end of the range, such as minInclusive only.
- The value for the lower bound (minInclusive or minExclusive) must be less than or equal to the value for the upper bound (maxInclusive or 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 the types derived from them. Special consideration should be given to time zones when applying bounds facets to date/time types. For more information, see Section 11.4.15 on p. 235.
8.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 XML DTD 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 as the number of items in the list. The facet value for length must be a nonnegative 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 nonnegative integers.
The three length facets (length, minLength, maxLength) can be applied to any string-based types (including the XML DTD types), the binary types, and anyURI. They cannot be applied to the date/time types, numeric types, or boolean.
8.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, base64Binary, and anyURI do not allow empty values unless xsi:nil appears in the element tag.
You may 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 type that includes both the integer type and an empty string, as shown in Example 8–8.
Example 8–8. Union allowing an empty value
<xs:simpleType name="DressSizeType"> <xs:union> <xs:simpleType> <xs:restriction base="xs:integer"> <xs:minInclusive value="2"/> <xs:maxInclusive value="18"/> </xs:restriction> </xs:simpleType> <xs:simpleType> <xs:restriction base="xs:token"> <xs:enumeration value=""/> </xs:restriction> </xs:simpleType> </xs:union> </xs:simpleType>
8.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 XML DTD types, the binary types, and anyURI. It does not make sense to try to limit the length of the date/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 8.4.1 on p. 142. You can also control the number of significant digits in an integer using the totalDigits facet, as discussed in Section 8.4.3 on p. 145. However, these facets do not consider leading zeros as significant. Therefore, they cannot force an integer to appear in the instance with a specific number of digits. To do this, you need a pattern. For example, the pattern \d{1,2} used in our DressSizeType 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 11.3.3.1 on p. 220 for a discussion of this issue.
8.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 nonnegative 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, as well as types derived from them. The fractionDigits facet may only be applied to decimal, because it is fixed at 0 for all integer types.
8.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 and assertion), 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 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 8–9 shows a simple type SMLXSizeType that allows the values small, medium, large, and extra large.
Example 8–9. Applying the enumeration facet
<xs:simpleType name="SMLXSizeType"> <xs:restriction base="xs:token"> <xs:enumeration value="small"/> <xs:enumeration value="medium"/> <xs:enumeration value="large"/> <xs:enumeration value="extra large"/> </xs:restriction> </xs:simpleType>
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 SMLSizeType to only be small, medium, and large, you could define a simple type as in Example 8–10.
Example 8–10. Restricting an enumeration
<xs:simpleType name="SMLSizeType"> <xs:restriction base="SMLXSizeType"> <xs:enumeration value="small"/> <xs:enumeration value="medium"/> <xs:enumeration value="large"/> </xs:restriction> </xs:simpleType>
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 SMLSizeType (small, medium, and large) are a subset of the values for SMLXSizeType. By contrast, Example 8–11 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 8–11. Illegal attempt to extend an enumeration
<xs:simpleType name="XSMLXSizeType"> <xs:restriction base="SMLXSizeType"> <xs:enumeration value="extra small"/> <xs:enumeration value="small"/> <xs:enumeration value="medium"/> <xs:enumeration value="large"/> <xs:enumeration value="extra large"/> </xs:restriction> </xs:simpleType>
The only way to add an enumeration value to a type is by defining a union type. Example 8–12 shows a union type that adds the value extra small to the set of valid values. Union types are described in detail in Section 10.2 on p. 183.
Example 8–12. Using a union to extend an enumeration
<xs:simpleType name="XSMLXSizeType"> <xs:union memberTypes="SMLXSizeType"> <xs:simpleType> <xs:restriction base="xs:token"> <xs:enumeration value="extra small"/> </xs:restriction> </xs:simpleType> </xs:union> </xs:simpleType>
When enumerating numbers, it is important to remember that the enumeration facet works on the actual value of the number, not its lexical representation as it appears in an XML instance. Example 8–13 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 11.7 on p. 253.
Example 8–13. Enumerating numeric values
Schema:
<xs:simpleType name="NewSmallDressSizeType"> <xs:restriction base="xs:integer"> <xs:enumeration value="2"/> <xs:enumeration value="4"/> <xs:enumeration value="6"/> </xs:restriction> </xs:simpleType>
Valid instances:
<size>2</size> <size>02</size>
The enumeration facet can be applied to any type except boolean.
8.4.5. Pattern
The pattern facet allows you to restrict values to a particular pattern, represented by a regular expression. Chapter 9 provides more detail on the rules for the regular expression syntax. Unlike most other facets (except enumeration and assertion), 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 8–14 shows a simple type DressSizeType that includes the pattern \d{1,2}, which restricts the size to one or two digits.
Example 8–14. Applying the pattern facet
<xs:simpleType name="DressSizeType"> <xs:restriction base="xs:integer"> <xs:minInclusive value="2"/> <xs:maxInclusive value="18"/> <xs:pattern value="\d{1,2}"/> </xs:restriction> </xs: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 8–15, 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 8–15. Restricting a pattern
<xs:simpleType name="SmallDressSizeType"> <xs:restriction base="DressSizeType"> <xs:minInclusive value="2"/> <xs:maxInclusive value="6"/> <xs:pattern value="\d{1}"/> </xs:restriction> </xs: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 facets of both the type and its ancestors, ensuring that it is in fact a subset. Example 8–16 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 LongerDressSizeType 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.
Example 8–16. Illegal attempt to extend a pattern
<xs:simpleType name="LongerDressSizeType"> <xs:restriction base="DressSizeType"> <xs:pattern value="\d{1,3}"/> </xs:restriction> </xs:simpleType>
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.
8.4.6. Assertion
The assertion facet allows you to specify additional constraints on values using XPath 2.0. Example 8–17 is a simple type with an assertion, namely that the value must be divisible by 2. It uses a facet named assertion with a test attribute that contains the XPath expression.
Simple type assertions are a flexible and powerful feature covered in more detail, along with complex type assertions, in Chapter 14.
Example 8–17. Simple type assertion
<xs:simpleType name="EvenDressSizeType"> <xs:restriction base="DressSizeType"> <xs:assertion test="$value mod 2 = 0" /> </xs:restriction> </xs:simpleType>
8.4.7. Explicit Time Zone
The explicitTimezone facet allows you to control the presence of an explicit time zone on a date/time value. Example 8–18 is a simple type based on time but with an explicit time zone required. The syntax of time zones is described in more detail in Section 11.4.13 on p. 233.
The value attribute of explicitTimezone has three possible values:
- optional, making the time zone optional (the value for most built-in date/time types)
- required, making the time zone required (the value for the dateTimeStamp built-in type)
- prohibited, disallowing the time zone
Example 8–18. Explicit time zone
<xs:simpleType name="SpecificTimeType"> <xs:restriction base="xs:time"> <xs:explicitTimezone value="required"/> </xs:restriction> </xs:simpleType>
8.4.8. 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 on to what to do with whitespace. This type of facet is known as a prelexical facet because it results in some processing of the value before the other constraining facets are applied. The valid values for the whiteSpace facet are:
- preserve: All whitespace is preserved; the value is not changed.
- replace: Each occurrence of tab (#x9), line feed (#xA), and carriage return (#xD) is replaced with a single space (#x20).
- 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.
Table 8-6 shows examples of how values of a string-based type will be handled depending on its whiteSpace facet.
Table 8-6. Handling of string values depending on whiteSpace facet
Original string |
string (preserve) |
normalizedString (replace) |
token (collapse) |
a string on two lines has spaces leading tab leading spaces |
a string on two lines has spaces leading tab leading spaces |
a string on two lines has spaces leading tab leading spaces |
a string on two lines has spaces leading tab leading spaces |
The whitespace processing, if any, will happen first, before any validation takes place. In Example 8–9, the base type of SMLXSizeType is token, which has a whiteSpace facet of collapse. Example 8–19 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 8–19. 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 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 it on string and applying a whiteSpace facet. Section 11.2.1 on p. 205 provides a discussion of these three types.