Home > Articles > Programming > Java

  • Print
  • + Share This
From the author of

Constraining Facets

Each datatype—both the primitive types outlined above and the derived types I'll discuss in a moment—have a number of properties that can be manipulated in order to limit or control the values that would be valid for a specific datatype.

For example, if you wanted to derive a new type called dollar to represent currency amounts in U.S. dollars, you could do this by using the decimal type as a base, and restricting the decimal places to a value of two. For example:

<xs:simpleType name="dollar">
 <xs:restriction base="decimal">
 <xs:fractionDigits value="2"/>
 </xs:restriction>
</xs:simpleType>

This would create a new derived type called dollar, which would be a decimal type but with the fractionDigits limited to two—so we could have 1.75 as a value, but not 1.75834. The specific constraining facets that can be used with each primitive datatype (or built-in derived datatype) can be found in Part 2: Datatypes, Section 4.3. Here is a summary of the most common facets, which apply to many different types:

  • length—The length facet is used to constrain the length of the value for a type. For example, a string's length is determined by the number of characters, and a decimal's length by the total number of digits. This facet can also be used with complex types, such as lists, where the length refers to the number of items in the list, rather than the number of characters.

  • minLength—The minLength facet can be used to express the minimum length of a value; for example, the minimum number of characters in a string or the minimum numeric value of a decimal.

  • maxLength—The maxLength facet can be used to express the maximum length of a value; for example, the maximum number of characters in a string or the maximum numeric value of a decimal.

  • pattern—The pattern facet is one of the most flexible and expressive constraining facets. This facet can be used to specify a regular expression that allows you to enforce strict pattern-matching against a value.

  • enumeration—The enumeration facet allows you to specify an enumerated attribute or enumerated elements, which are essentially just a list of valid values from which the user must choose one value.

These basic constraining facets provide a mechanism for customizing the primitive datatypes to create new types, called derived datatypes. The dollar type created earlier is an example of a derived datatype, and I'll discuss the methods for derivation later.

  • + Share This
  • 🔖 Save To Your Account