[Archive copy mirrored from: http://www.textuality.com/xml/typing.html, May 21, 1997]
SGML and XML ("XML" refers to both from here on in) provide facilities for declaring document structures. However, there is very limited support for data typing as a database person would see it. This is an obvious deficiency whose seriousness will increase as XML is used increasingly for electronic data interchange and database-related applications.
This note proposes a mechanism to attach strong type declarations to XML elements using reserved attributes. While this is similar to HyTime's "architectural form" mechanism, this note does not include assume understanding, nor provide any discussion, of that mechanism.
This specification is designed for use with XML documents as described in the document Extensible Markup Language - the special terms element, attribute, and character data have the meanings defined in that document, and the syntax and usage of the element and attribute declarations is that described in that document.
However, the mechanisms described here may be applied to SGML documents (given appropriate declarations) and perhaps to HTML documents.
SQL, as defined in International Standard ISO/IEC 9075:1992, is a language designed for use in defining and accessing structured data repositories. It includes a comprehensive selection of data types: see 6.1 <data type>. This selection has been proven effective in practice.
This note provides XML mechanisms for declaring elements to be one of a subset of these SQL types, and for restricting the range of allowed values for numeric types.
The ISO standard 8601:1988, which supersedes ISO standards 2014, 2015, 2711, 3307 and 4031, describes numerical date and time interchange formats. Dates are those used in the Gregorian calendar.
Each datum is fixed in size (leading zeroes used as necessary) and presented in decreasing order of significance (year, month, day, hour, minute, second, fractional second). All characters are taken from the ASCII repertoire.
In this specification, only the complete date and time values described in ISO 8601:1988 are used, with none of the truncation, reduced precision, or current-century tpyes of omission.
SQL data types may be associated with SQL elements based on the use of the reserved attributes XML-SQLTYPE
, whose value corresponds to the name of a SQL data type. The declaration for this attribute is:
<!ELEMENT AnyElement (#PCDATA)> |
In some cases, a single attribute is not sufficient to provide all the required typing contraints. When this is the case, other attributes may be used to control quantities such as the size and scale of the data item. These attributes, all of whose names begin XML-SQL
, are described in the sections below that discuss the details associated with each possible value of XML-SQLTYPE
.
In the declaration above, XML-SQLTYPE
is #IMPLIED
; in practice, one would expect this to be given a #FIXED
default in the DTD, so that all instances of some element would have the same type. When the XML-SQLTYPE
attribute is not provided for some element, this simply means that no assertion is made concerning the data type of that element.
Elements for which the type is constrained with XML-SQLTYPE
may have ranges of validity declared using the attributes XML-SQLMIN
and XML-SQLMAX
. These have no defaults of any kind; if not provided, no range constraint is placed on the content.
<!ATTLIST AnyElement XML-SQLMIN CDATA #IMPLIED |
In all cases, the value of XML-SQLMIN
and XML-SQLMAX
must meet the constraints expressed by XML-SQLTYPE
and any other paramaterizing attributes.
For numeric, date, and time data types, the ordering is unambiguous and the interpretation of XML-SQLMIN
and XML-SQLMAX
is obvious. For the CHAR
and VARCHAR
data types, the lexical ordering of strings is often implementation dependent. While the ordering of strings made up of characters from the ASCII and ISO-Latin character sets is well-understood, this is not the case with Unicode characters representing the glyphs of many Asian languages.
Confusion is also possible due to the fact that the direction in which characters are visually rendered into strings varies from language to language (Arabic runs right-to-left) and even within languages (Chinese may be rendered validly in many different directions).
To avoid ambiguity, for XML-SQLMIN
and XML-SQLMAX
range checking of CHAR
and VARCHAR
elements, lexical comparison of strings must always be done using the numeric values of the Unicode encoding of the characters in the string, in increasing order of the address at which they are stored.
The attributes described in this note may only be attached to elements with #PCDATA
content; that is, those which have no child elements.
In this section, the term content refers to the character data contained in an element.
A type declaration of the form described in this note asserts that the content of some element should meet the constraints (described herein) expressed by that declaration.
Several of the type declarations constrain the allowed length of the content. In this case, the length is in characters, and should be evaluated after all entity and character references have been processed; i.e. the count applies to the content as received by an application, not as encoded in the containing entity.
This section and each one following begins with an example of the declaration and use of an SQL-typed element.
<!ELEMENT CHAR-datum (#PCDATA)> |
The content is fixed in length.
XML-SQLSIZE
must be provided, either directly or by default.XML-SQLSIZE
must match: digit+
XML-SQLSIZE
.XML-SQLMIN
is provided, it must be a character string of the length given by XML-SQLSIZE
. The content must be lexically greater than or equal to the value of XML-SQLMIN
.XML-SQLMAX
is provided, it must be a character string of the length given by XML-SQLSIZE
. The content must be lexically less than or equal to the value of XML-SQLMAX
.
<!ELEMENT VARCHAR-datum (#PCDATA)> |
The content is variable in length up to a fixed maximum.
XML-SQLSIZE
must be provided, either directly or by default.XML-SQLSIZE
must match: digit+
XML-SQLSIZE
.XML-SQLSIZE
.XML-SQLMIN
is provided, it must be a character string of the length less than or equal to that given by XML-SQLSIZE
. The content must be lexically greater than or equal to the value of XML-SQLMIN
.XML-SQLMAX
is provided, it must be a character string of the length less than or equal to that given by XML-SQLSIZE
. The content must be lexically less than or equal to the value of XML-SQLMAX
.
<!ELEMENT INTEGER-datum (#PCDATA)> |
The content represents a decimal integer number.
[+-]? digit+
XML-SQLMIN
is provided, it must be an integer. The content, interpreted as a number, must be greater than or equal to the value of XML-SQLMIN
.XML-SQLMAX
is provided, it must be an integer. The content, interpreted as a number, must be less than or equal to the value of XML-SQLMAX
.
<!ELEMENT DECIMAL-datum (#PCDATA)> |
The content represents a fixed-point decimal number with a fixed number of digits after the decimal point.
[+-]? digit+ '.' digit*
XML-SQLSCALE
must be provided, either directly or by default.XML-SQLSCALE
must match: digit+
XML-SQLSCALE
provides the SQL scale of the content's numeric value; the number of digits following the decimal point must be equal to that scale.XML-SQLMIN
is provided, it must be a decimal number with the same scale as the content. The content, interpreted as a number, must be greater than or equal to the value of XML-SQLMIN
.XML-SQLMAX
is provided, it must be a decimal number with the same scale as the content. The content, interpreted as a number, must be less than or equal to the value of XML-SQLMAX
.
<!ELEMENT FLOAT-datum (#PCDATA)> |
The content represents a floating-point number.
[+-]? digit+ '.' digit+ ([eE] [+-]? digit+)?
XML-SQLMIN
is provided, it must be a floating-point number. The content, interpreted as a number, must be greater than or equal to the value of XML-SQLMIN
.XML-SQLMAX
is provided, it must be a floating-point number. The content, interpreted as a number, must be less than or equal to the value of XML-SQLMAX
.
<!ELEMENT DATE-datum (#PCDATA)> |
The content represents a date, provided in the order Year, Month, Day.
digit digit digit digit '-' digit digit '-' digit digit
0000
and 9999
inclusive are allowed.01
and 12
inclusive are allowed.01
and 31
inclusive are allowed.XML-SQLMIN
is provided, it must represent a date as described immediately above. The content, interpreted as a date, must be later than or equal to the value of XML-SQLMIN
.XML-SQLMAX
is provided, it must represent a date as described immediately above. The content, interpreted as a date, must be earlier than or equal to the value of XML-SQLMAX
.
<!ELEMENT TIME-datum (#PCDATA)> |
Describes a time of day.
XML-SQLTZ
is has the value YES
, this signals that the timezone value, described below, must be provided in the content. In SQL terms, the value YES
is equivalent to the declaration fragment WITH TIME ZONE
. digit digit ':' digit digit ':' digit digit ('.' digit+)? ([+-] digit digit ('.' digit digit)? )?
00
and 23
inclusive are allowed.00
and 59
inclusive are allowed.00
and 59
inclusive are allowed. The trailing decimal point and digits, if provided, represent a portion of a second as a decimal fraction.+
' or '-
' sign) indicates a displacement from Co-ordinated Universal Time. It may be provided only if XML-SQLTZ
has the value YES
.+
or -
sign, signaling the direction of displacement from Co-ordinated Universal Time. 00
and 13
inclusive are allowed.00
and 59
inclusive are allowed.XML-SQLMIN
is provided, it must represent a time as described immediately above. The content, interpreted as a time of day, must be later than or equal to that given in XML-SQLMIN
.XML-SQLMAX
is provided, it must represent a time as described immediately above. The content, interpreted as a time of day, must be earlier than or equal to that given in XML-SQLMAX
.
<!ELEMENT TIMESTAMP-datum (#PCDATA)> |
Describes a timestamp, including both date and time.
XML-SQLSIZE
is identical to that for XML-SQLTYPE="TIME"
.XML-SQLTYPE="DATE"
, followed by the character 'T
', followed by a string which would be valid as content of an element with XML-SQLTYPE="TIME"
.XML-SQLMIN
is provided, it must represent a timestamp as described immediately above. The content, interpreted as a timestamp, must be later than or equal to that given in XML-SQLMIN
.XML-SQLMAX
is provided, it must represent a timestamp as described immediately above. The content, interpreted as a timestamp, must be earlier than or equal to that given in XML-SQLMAX
.For a bank loan; balance, interest rate, and maturity date:
<!ELEMENT BALANCE (#PCDATA) > |
For an airline departure: passenger name, seat number, and departure time:
<!ELEMENT LAST-NAME (#PCDATA)> |