Microsoft XML 2.5 SDK


 

Lesson 7: Using Data Types Within XML Documents

[This is preliminary documentation and subject to change.]

What is a data type within 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 will have to conform to that data type. This provides you with a method of 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:

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

can have both a value of "1970-09-30" and a typed value of "Wed Sep 30 00:00:00 PDT 1970."

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 schema above 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 within 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.

Datatypes as elements within 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>

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>

It is not possible to type such elements through schema.

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