Home > Articles > Web Services > XML

Simple Types

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Facets

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:

  • 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 minInclusiveand 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 types derived from them. Special consideration should be given to time zones when applying bounds facets to date and time types.

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.

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 base64-Binary 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> 

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 the section called "Bounds Facets." You can also control the number of significant digits in an integer using the totalDigits facet, as discussed in the following section called "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.

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.

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 8 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 9.

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 10 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 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 9 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 10 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 11 shows a union type that adds the value extra small to the set of valid values.

Example 11 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 12 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. The enumeration facet can be applied to any type except boolean.

Pattern

The pattern facet allows you to restrict values to a particular pattern, represented by a regular expression. 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 12 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 13 shows a simple type (DressSizeType) that includes the pattern \d{1,2}, which restricts the size to one or two digits.

Example 13 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 14, 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 14 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 15 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 15 Illegal Attempt to Extend a Pattern

<xsd:simpleType name="LongerDressSizeType">
 <xsd:restriction base="DressSizeType">
  <xsd:pattern value="\d{1,3}"/> 
 </xsd:restriction>
</xsd:simpleType> 

Whitespace

The whiteSpace facet allows you to specify the whitespace normalization rules that 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 9–6 shows examples of how values of a string-based type will be handled depending on its whiteSpace facet.

Table 6 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
two lines

on
two lines

on two lines

on 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 8, the base type of SMLXSizeType is token, which has a whiteSpace facet of collapse. Example 16 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 16 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.

  • + Share This
  • 🔖 Save To Your Account

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