Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning Regular Expressions 2005.pdf
Скачиваний:
95
Добавлен:
17.08.2013
Размер:
25.42 Mб
Скачать

Chapter 24

Figure 24-5

How Constraints Are Expressed in W3C XML Schema

In one sense, W3C XML Schema is all about applying constraints. One type of constraint is limiting how elements and attributes can be structured inside an XML instance document belonging to the class of XML documents to which the schema applies. Another aspect of W3C XML Schema constraining the content of a class of XML documents is in constraining the content allowed as the value contained in an element or attribute.

Two kinds of types can exist as the content of an element: a complex type (indicated by an xs:complexType element in the schema) and a simple type (which may be indicated by an xs:simpleType element in the schema). This chapter focuses on constraining the values allowed in simple types in an XML instance document.

598

Regular Expressions in W3C XML Schema

W3C XML Schema Datatypes

In the other uses of regular expressions you have seen in this book, the regular expression has been applied to a string value. In W3C XML Schema, it is possible to use regular expressions together with other datatypes.

The following table summarizes the datatypes built into W3C XML Schema. Datatypes are shown as having the xs namespace prefix as an indication that they belong to the XML namespace http://www.w3.org/ 2001/XMLSchema. Datatypes can be viewed as primitive or derived built-in datatypes.

Datatype

Description

 

 

xs:anyType

Functions as the root of the type hierarchy. Types derived from

 

xs:anyType can be a complex type or a simple type.

xs:anySimpleType

The base type for all simple types.

xs:string

A sequence of XML characters of finite length.

xs:boolean

Expresses the binary notion of true and false.

xs:base64Binary

Represents base-64 encoded binary data.

xs:hexBinary

Represents hexadecimal encoded binary data.

xs:float

Represents an IEEE single-precision 32-bit floating-point number.

xs:decimal

Represents arbitrary precision decimal numbers.

xs:double

Represents an IEEE double-precision 64-bit floating-point number.

xs:anyURI

Represents a Uniform Resource Identifier, whether absolute or rela-

 

tive, and may include a fragment identifier.

xs:QName

An XML namespace-qualified name.

xs:NOTATION

Represents an XML 1.0 NOTATION.

xs:duration

Represents a duration with Gregorian year, month, day, hour,

 

minute, and seconds components.

xs:dateTime

Represents a specific instant of time.

xs:time

Represents a specific instant of time that recurs every day.

xs:date

Represents a specified calendar day.

xs:gYearMonth

Represents the year and month parts of an xs:dateTime.

xs:gMonthDay

Represents a specified day of the year, such as September 25.

xs:gDay

Represents a specified day of the month, such as the 25th.

xs:gMonth

Represents a specified Gregorian calendar month.

 

 

In addition to the datatypes already listed, there are datatypes derived, directly or indirectly, from the xs:string and xs:decimal datatypes.

599

Chapter 24

The following table summarizes the datatypes that are derived from xs:string.

Derived Datatype

Description

 

 

xs:normalizedString

The base type is xs:string. The xs:normalizedString type is the

 

set of strings that does not contain the characters carriage return

 

(#xD), linefeed (#xA), and tab (#x9).

xs:token

The base type is xs:string. This datatype is the set of strings that

 

does not contain the linefeed (#xA) or tab (#x9) characters, nor any

 

leading or trailing space characters (#x20) or any doubled internal

 

space characters.

xs:language

The base type is xs:token. This datatype is the set of xs:token val-

 

ues that are language identifiers in the XML 1.0 (second edition)

 

specification.

xs:Name

The base type is xs:token. This datatype is the set of strings that

 

are legal XML names, as defined in the XML 1.0 (second edition)

 

specification.

xs:NCName

The base type is xs:Name. This datatype is the set of strings that are

 

XML names but do not contain a colon character.

xs:ID

The base type is xs:NCName. This datatype represents values of ID

 

type that are also NCNames.

xs:IDREF

The base type is xs:NCName. This datatype is the set of strings that

 

represent values of type IDREF, which are NCNames.

xs:IDREFS

The item type is xs:IDREF. This datatype is a list of whitespace-sep-

 

arated values, each of which is of type xs:IDREF.

xs:NMTOKEN

The base type is xs:token. This datatype is the set of xs:token val-

 

ues that match the NMTOKEN definition in XML 1.0 (second edition).

xs:NMTOKENS

The item type is xs:NMTOKEN. This datatype is a list of whitespace-

 

separated values, each of which is of type xs:NMTOKEN.

xs:ENTITY

The base type is xs:NCName. This datatype represents values that are of

 

ENTITY type, as defined in the XML 1.0 (second edition) specification.

xs:ENTITIES

The item type is xs:ENTITY. This datatype is a list of whitespace-

 

separated values, each of which is of type xs:ENTITY.

 

 

The following table summarizes the built-in datatypes that are derived, directly or indirectly, from the xs:decimal datatype.

600

 

 

Regular Expressions in W3C XML Schema

 

 

 

 

Derived Datatype

Description

 

 

 

 

xs:integer

The base type is xs:decimal. This datatype represents positive and

 

 

negative integer values.

 

xs:nonPositiveInteger

The base type is xs:integer. This datatype represents negative inte-

 

 

gers and zero.

 

xs:negativeInteger

The base type is xs:nonPositiveInteger. This datatype represents

 

 

negative integers.

 

xs:long

The base type is xs:integer. This datatype represents integer val-

 

 

ues from -9223372036854775808 to 9223372036854775807.

 

xs:int

The base type is xs:long. This datatype represents integer values

 

 

from -2147483648 to 2147483647 inclusive.

 

xs:short

The base type is xs:int. This datatype represents integer values

 

 

from -32768 to 32767 inclusive.

 

xs:byte

The base type is xs:short. This datatype represents integer values

 

 

from -128 to 127 inclusive.

 

xs:nonNegativeInteger

The base type is xs:integer. This datatype represents integer val-

 

 

ues that are positive integers and zero.

 

xs:unsignedLong

The base type is xs:nonNegativeInteger. This datatype represents

 

 

integer values from 0 to 18446744073709551615.

 

xs:unsignedInt

The base type is xs:unsignedLong. This datatype represents integer

 

 

values from 0 to 4294967295 inclusive.

 

xs:unsignedShort

The base type is xs:unsignedInt. This datatype represents integer

 

 

values from 0 to 65535 inclusive.

 

xs:unsignedByte

The base type is xs:unsignedShort. This datatype represents inte-

 

 

ger values from 0 to 255 inclusive.

 

xs:positiveInteger

The base type is xs:nonNegativeInteger. This datatype represents

 

 

integer values of 1 and greater.

 

 

 

Fuller details on how the built-in datatypes are specified can be found in XML Schema Part 2 at www.w3.org/TR/2001/REC-xmlschema-2-20010502, XML 1.0 (second edition) at www.w3.org/TR/ 2000/WD-xml-2e-20000814, and Namespaces in XML at www.w3.org/TR/REC-xml-names.

The programmer can develop custom types from these built-in types by any of the three mechanisms in the following list:

Derivation by restriction — Values of an existing datatype are constrained by restricting the allowed values.

Derivation by list — A list of values of a built-in or user-defined datatype.

Derivation by union — The user-defined datatype is the union of two other datatypes (which can be built-in datatypes or user-defined datatypes).

601