Use Data Types Within XML Documents
This topic discusses data types in XML documents.
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."
Consider the following XML document
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.
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.
The dt prefix can now be used on the dt attribute to assign a data type to an instance of an element.
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.
The second example declares the types as elements.
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.