XML technologies / XML Schema / Using predefined simple data types / String data types

(Excerpt from “XML Schema” by Eric van der Vlist)

This page discusses data types derived from the primitive type xs:string and other types with similar behavior, namely xs:hexBinary, xs:base64Binary, restrictions, xs:QName and xs:NOTATION. These types are not expected to have a quantifiable value; W3C XML Schema does not even expect to sort them. Unless stated otherwise, their value and lexical spaces are identical. Although grouped here for their similar behavior, these primitive types are considered very different by the Recommendation.

The types discussed here are shown in the following figure.

The two exceptions to whitespace processing, xs:string and xs:normalizedString, are string types. One main difference between these types is their whitespace processing. To emphasize this, we classify them by how they process whitespace.

No whitespace replacement

xs:string

This string type is the only predefined type with no whitespace replacement. As the next chapter shows, custom types derived from it can set whitespace replacement without restriction. Conversely, a custom type cannot disable replacement when derived from any predefined type other than xs:string.

Strings and similar data types

Figure: Strings and similar data types

As expected, a string is a sequence of characters satisfying XML 1.0’s definition: permitted characters are tab, carriage return, linefeed and the characters allowed by Unicode and ISO/IEC 10646.

The value of the following element:

<title lang="de">
Auf den
Hund gekommen
</title>

is the full string:

Auf den
Hund gekommen

including all tabs and carriage returns/linefeeds, if the element title has type xs:string.

Normalized strings

xs:normalizedString

The normalized string is the only predefined type where whitespace is replaced without collapsing.

The lexical space of xs:normalizedString is the same as that of its base type xs:string, with one exception: each #x9, tab, #xA, linefeed, and #xD, carriage return, becomes exactly one #x20, space. These three characters therefore occur in neither the lexical nor the value space.

The value of the same element:

<title lang="de"><title lang="de">
  Auf den
  Hund gekommen
</title>

is now the string:

Auf den   Hund gekommen

with every whitespace character replaced by a space, if the element title has type xs:normalizedString.

Normalized strings have no additional restrictions. Every valid xs:string is also a valid xs:normalizedString. The difference is the whitespace processing used to determine the lexical value.

Collapsed strings

Whitespace collapsing follows replacement: leading and trailing spaces are removed, and each consecutive sequence of spaces becomes one space. All predefined types, except xs:string and xs:normalizedString, as we saw, have collapsed whitespace.

We place tokens, binary formats, URIs, qualified names, notations and their derived types in this category. Despite their shared properties, this grouping is explanatory only and does not appear directly in the Recommendation.

Tokens

xs:token

xs:token is xs:normalizedString with collapsed whitespace. Since whitespace can still occur in the lexical space of xs:token, “tokenized” would be a better name than simply “token”.

The same element:

<title lang="de">
  Auf den
  Hund gekommen
</title>

is still a valid xs:token, now having the string value:

Auf den Hund gekommen

where whitespace was first replaced by spaces, trailing spaces removed and consecutive spaces replaced by single spaces.

Just as with xs:normalizedString, there is no restriction on xs:token, and every valid xs:string is also a valid xs:token. The difference is the whitespace processing used to determine the lexical value. This does not apply to derived types imposing additional lexical- and value-space constraints. The lexical restriction of xs:normalizedString is thus a projection of its parsed space: several parsed values become the same lexical value. It does not invalidate lexical values as restrictions on other predefined types do.

The predefined types derived from xs:token are xs:language, xs:NM -TOKEN and xs:Name.

xs:language

This type accepts all language codes standardized by RFC 1766. Valid examples include en, en-US, de or de-DE .

xs:NMTOKEN

This corresponds to XML 1.0’s “Nmtoken” production: one token, a character sequence without spaces, made of characters permitted in XML names. Valid examples are “Snoopy”, “CMS”, “1950-10-04” or “3810518883”. Invalid examples include “brachte die klassische Musik in die Peanuts-Comics ein”, spaces are forbidden, or “kühn,dreist”, commas are forbidden.

xs:Name

This resembles xs:NMTOKEN but additionally requires values to begin with a letter, “:” or “-”. It corresponds to XML 1.0’s “Name” definition. Valid examples include “Snoopy”, “CMS” or “-1950-10-04-10:00”. Invalid examples include “3810518883”, since xs:Name cannot begin with a digit, or “kühn,dreist”, commas are forbidden. This type should not be used for names that can be qualified by a namespace-prefix, since another type, xs:QName, provides special semantics for those values. The type xs:NCName derives from xs:Name.

xs:NCName

This is the noncolonized name, defined by the Recommendation Namespaces in XML 1.0: an xs:Name without any colon, “:”. This predefined type probably best matches most programming languages’ idea of a name, although characters such as “-” and “.” may still cause problems. Valid values include “Snoopy”, “CMS” or “-1950-10-04-10-00”. Invalid values include “-1950-10-04:10-00 ” or “kühn:dreist”, colons are forbidden. xs:ID, xs:IDREF and xs:ENTITY derive from xs:NCName.

xs:ID

This type derives from xs:NCName. Its value space additionally prohibits duplicate values within a document. Thus attributes or simple-type elements with this type can serve as unique identifiers. It emulates XML 1.0’s ID attribute type. This is examined in detail in Defining uniqueness, keys and key references.

xs:IDREF

This type derives from xs:NCName. Its value space requires the value to match an ID defined in the same document.

xs:ENTITY

This too exists for compatibility with XML 1.0 DTDs. It derives from xs:NCName and must match an unparsed entity defined in a DTD.

XML 1.0 defines unparsed entities as resources whose content may or may not be text; if text, it need not be XML. Each has an associated notation identified by name. Apart from requiring an XML processor to give the application identifiers for the entity and notation, XML imposes no constraints on unparsed entity content. In practice this mechanism is rarely used: links to resources are normally used instead.

Qualified names

xs:QName

In accordance with the Recommendation Namespaces in XML 1.0, xs:QName supports names with namespace prefixes. An xs:QName with a namespace prefix uses shorthand to identify a URI. Each xs:QName effectively contains tuples of the form {namespace name, local part}, where the namespace name is the URI associated with the prefix by a namespace declaration. Although the lexical space of xs:QName closely resembles that of xs:Name, the only additional restriction being at most one colon in an xs:QName and never in first position, their value spaces differ completely: a scalar for xs:Name versus a tuple for xs:QName. Also, xs:QName is defined as a primitive type. Its additional restriction over xs:Name is that the prefix must be declared as a namespace prefix in the scope of the element using the type.

W3C XML Schema has already provided QName examples. When we write <xs:attribute name="lang" type="xs:language"/>, the type attribute is an xs:QName whose value is the tuple:

{"http://www.w3.org/2001/XMLSchema", "language"}

The prefix xs: is associated with URI "http://www.w3.org/2001/XMLSchema". Without a namespace declaration for this prefix, the attribute type is considered invalid.

The prefix of an xs:QName is optional. We can also write:

<xs:element ref="book" maxOccurs="unbounded"/>

Here, the attribute ref is again an xs:QName with tuple value:

{NULL, "book"}

After all, we have defined no default namespace. xs:QName does support default namespaces, however. If a default namespace is in scope for the element, its URI supplies the tuple’s namespace value.

URIs

Einschränkungen

This is another string type with different lexical and value spaces. It compensates for formatting differences between XML and URIs under RFCs 2396 and 2732. These RFCs are unfriendly to non-ASCII characters and require many escapes unnecessary in XML. The W3C XML Schema Recommendation does not specify the transformation, only noting its similarity to the one described for XLink link locators.

For example, the attribute href on an XHTML link written as:

<a href="http://dmoz.org/World/Français/">
  Word/Français
</a>

is transformed into this value-space value:

"http://dmoz.org/World/Fran%c3%a7ais/"

The type Einschränkungen ignores any xml:base attributes defined in the document.

Notations

xs:NOTATION

This is probably the most obscure string type, created for XML 1.0 notations. It cannot be used directly in a schema, only through custom derived types. The next chapter explains further.

String-encoded binary data types

XML 1.0 cannot contain binary content, so it must be encoded as strings before insertion into XML documents. W3C XML Schema defines two primitive types for two common encodings, hexBinary and base64. These can hold arbitrary binary content, including text incompatible with XML markup. Other binary-to-text encodings, such as uuXXcode, Quoted Printable, BinHex, aencode and base85, can also be used, but their values are not recognized by W3C XML Schema.

xs:hexBinary

This defines a simple binary-to-string encoding, converting each binary octet to two hexadecimal digits. It differs from BinHex, introduced by Apple and described in RFC 1741, which compresses repeated characters.

An XML header in UTF-8 such as:

<?xml version="1.0" encoding="UTF-8"?>

Encoded as xs:hexBinary, it becomes:

3c3f786d6c2076657273696f6e3d22312e302220656e636f64696e673d225554462d38223f3e

xs:base64Binary

This is the encoding known as base64, described in RFC 2045. It maps groups of six bits to an array of 64 printable characters.

The same header, encoded as xs:base64Binary, becomes:

PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4=

The W3C XML Schema Recommendation overlooked RFC 2045’s requirement of a line break every 76 characters. An erratum should clarify this. Since W3C XML Schema treats these breaks as optional, the lexical and value spaces of xs:base64Binary cannot be considered identical.

   

 

 

 

back to top