Home > Articles > Web Services > XML

This chapter is from the book

This chapter is from the book

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:

  1. optional, making the time zone optional (the value for most built-in date/time types)
  2. required, making the time zone required (the value for the dateTimeStamp built-in type)
  3. 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.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020