Use Data Types Within XML Documents

 

This topic discusses data types in XML documents.

What Is a Data Type in an XML Document?

A data type within an XML document is a type that has been assigned to an element on the instance using the dt:dt attribute, or through an XML Schema, a formal definition of an XML document. In addition, data types can be declared as elements. The XML parser uses the data type information to validate the document.

Previously, XML element values were limited to a single type, string, so developers who wanted to process XML documents had to spend time converting all values to a type within their own applications. Typing your XML data will let the parser take care of data type conversions. In addition, because values are assigned a particular data type, changes made to that value must conform to that data type. This provides you with a method for validating user input.

In addition to having a string value, each XML element can also have a typed value. For example, the following XML element can have both a value of "1970-09-30" and a typed value of "Wed Sep 30 00:00:00 PDT 1970."

<date>1970-09-30</date>

How Do I Type XML Element Values?

Consider the following XML document

<?xml version="1.0"?>
<weather xmlns="x-schema:weatherSchema.xml">
  <date>1970-09-30</date>
  <degrees>67.5</degrees>
</weather>

where weatherSchema.xml is the following file:

<Schema xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemas-microsoft-com:datatypes">
  <ElementType name="date" content="textOnly" dt:type="date"/>
  <ElementType name="degrees" content="textOnly" dt:type="float"/>
  <ElementType name="weather" content="eltOnly">
    <element type="date"/>
    <element type="degrees"/>
  </ElementType>
</Schema>

The preceding schema is a formal definition assigning data types to the elements of the preceding XML document. The attribute xmlns is the XML keyword for a namespace declaration. The declaration xmlns:dt="urn:schemas-microsoft-com:datatypes" assigns dt to the namespace urn:schemas-microsoft-com:datatypes. Any type qualified with dt will be from the namespace urn:schemas-microsoft-com:datatypes.

The schema and data type namespaces are declared at the beginning of the XML Schema so that the dt prefix can then be used to denote which type attributes hold data type designations.

<Schema xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemas-microsoft-com:datatypes">
  <ElementType name="NUMBER" content="textOnly" dt:type="number"/>

Typing an Element in the XML Document

An element can also be typed through the dt attribute on the instance of the element. To do this, declare the data type namespace at the beginning of the XML document.

<NUMBERS xmlns:dt="urn:schemas-microsoft-com:datatypes">
  <NUMBER dt:dt="number">44533</NUMBER>
</NUMBERS>

The dt prefix can now be used on the dt attribute to assign a data type to an instance of an element.

Data Types as Elements in the XML Document

In addition to declaring data types on the instance of an element and in a schema, they can also be used as elements. Compare the following examples. The first declares data types on the instance of an element.

<shoes xmlns:dt="urn:schemas-microsoft-com:datatypes" id="f1121" sizes="mens">
<sizes id="mens">
  <size dt:dt="int">8</size>
  <size dt:dt="int">10</size>
  <size dt:dt="int">12</size>
</sizes>
</shoes>

The second example declares the types as elements.

<shoes xmlns:dt="urn:schemas-microsoft-com:datatypes" id="f1121" sizes="array1">
<array id="array1">
  <dt:int>8</dt:int>
  <dt:int>10</dt:int>
  <dt:int>12</dt:int>
</array>
</shoes>

It is not possible to type such elements through schema.

For a list of data types that you can use in your XML documents, see XDR Schema Data Types Reference.