[Mirrored from: http://www.textuality.com/xml/typing.html, May 15, 1997. See the updated (May 21) version.]
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.
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.
SQL data types may be associated with SQL elements based on the use of two reserved attributes, XML-SQLTYPE
and XML-SQLSIZE
. The value of the XML-SQLTYPE
corresponds to the name of a SQL data type. The value of the XML-SQLSIZE
attribte is a number, which parameterizes the type. Its interpretation depends on the value of the XML-SQLTYPE
attribute. Although this attribute conflates several distinct SQL concepts, its usage is unambiguous in the context of the declared data type.
The SGML declaration for these attributes is given below; we use an SGML rather than an XML declaration as a convenient method of expressing the constraint that the value of XML-SQLSIZE
must be numeric.
<!ELEMENT AnyElement (#PCDATA)> |
In the example 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.
If XML-SQLTYPE
is provided but XML-SQLSIZE
is not, the value is assumed to be zero, as declared above. This value makes sense in the one case (TIME
) where it is desirable to have a default, and is sufficiently unusable for character and numeric types as to require that it be provided, without having to write that requirement into this document.
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 |
In all cases, the value of XML-SQLMIN
and XML-SQLMAX
must meet the constraints expressed by XML-SQLTYPE
and XML-SQLSIZE
.
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.
The content is fixed in length.
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
.The content is variable in length up to a fixed maximum.
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
.The content represents a decimal integer number.
XML-SQLSIZE
is ignored.('+'|'-')? 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
.The content represents a fixed-point decimal number with a fixed number of digits after the decimal point.
('+'|'-')? digit+ '.' digit*
XML-SQLSIZE
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
.The content represents a floating-point number.
XML-SQLSIZE
is ignored.('+'|'-')? digit+ '.' 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
.The content represents a date, provided in the order Year, Month, Day.
XML-SQLSIZE
is ignored.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
.Describes a time of day.
XML-SQLSIZE
is provided with any value other than the default 0, this signals that the timezone value, described below, must be provided in the content. In SQL terms, a nonzero value 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.XML-SQLSIZE
has a nonzero value.+
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
.Describes a timestamp, including both date and time.
XML-SQLSIZE
is identical to that for XML-SQLTYPE="TIME"
.XML-SQLTYPE="DATE"
, followed by a space, 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)> |