Specifying Data Types

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XML Schemas

Specifying Data Types

Although XML-Data Reduced (XDR) schema support in MSXML 2.0 and later versions allow data types to be specified within attributes, only the following data types are supported within attributes by MSXML and the Document Object Model (DOM):

  • string
  • id
  • idref
  • idrefs
  • nmtoken
  • nmtokens
  • enumeration

In MSXML 2.0, the list of data types that can apply to elements did not include the attribute types. These restrictions were lifted in MSXML 2.5 (released with Microsoft Windows® 2000 and Internet Explorer 5.01) although attribute type data types other than id, idref, and idrefs on elements were not validated. With MSXML 2.6 and later, attributes and elements have equivalent functionality.

Example A — Specifying the Data Type in the Schema

The following schema examples specify data types, such as string, nmtoken, int, float, Boolean, and so on, when declaring an element or attribute.

In this example, the dt:type attribute declares the data type of the att1 attribute.

<?xml version="1.0" ?>
<Schema xmlns="urn:schemas-microsoft-com:xml-data"
        xmlns:dt="urn:schemas-microsoft-com:datatypes" >
  <AttributeType name="att1" dt:type="int"/>
  <ElementType name="Element1">
    <element type="Element1"/>
    <attribute type="att1"/>
  </ElementType>
</Schema>

<?xml version="1.0" ?>
<r xmlns:a="x-schema:abovefile.xml">
  <a:Element1 a:att1=”22”>
  </a:Element1>
</r>
Alternatively, you can specify the data type using the <datatype> element as in this example.
<AttributeType name="att1">
    <datatype dt:type="int"/>
</AttributeType>

Example B — Specifying id/idref/idrefs Types in the Schema

The following example illustrates how the id, idref, and idrefs attribute data types of a document type definition (DTD) are specified in XDR schema. The schema describes the Employee and Order elements.

Because an employee takes orders, the OrderList attribute of the Employee element is declared as idrefs type, referring to the OrderID attribute of the Order element. Conversely, because an order is taken by an employee, the EmployeeID attribute of the Order element is declared as idref type, referring to the EmployeeID attribute of the Employee element.

<?xml version="1.0" ?>
<Schema xmlns="urn:schemas-microsoft-com:xml-data"
        xmlns:dt="urn:schemas-microsoft-com:datatypes" >
  <ElementType name="Order" >
    <AttributeType name="OrderID" dt:type="id"/>
    <AttributeType name="OrderDate"/>
    <AttributeType name="EmployeeID" dt:type="idref"/>
 
    <attribute type="OrderID"/>
    <attribute type="OrderDate"/>
    <attribute type="EmployeeID"/>
  </ElementType>
  <ElementType name="Employee">
    <AttributeType name="EmployeeID" dt:type="id"/>
    <AttributeType name="LastName"/>

    <attribute type="EmployeeID"/>
    <attribute type="LastName"/>
    <AttributeType name="OrderList" dt:type="idrefs"/>
    <attribute type="OrderList"/>
  </ElementType>
</Schema>

The following is a roughly equivalent DTD.

  <!ELEMENT Order EMPTY >
  <!ATTLIST Order
            OrderID    ID    #IMPLIED
            OrderDate  CDATA #IMPLIED
            EmployeeID IDREF #IMPLIED >

  <!ELEMENT Employee EMPTY>
  <!ATTLIST Employee 
            Employee  IDID  #IMPLIED
            LastName  CDATA  #IMPLIED 
            OrderList IDREFS #IMPLIED >

An instance of the XML fragment is shown in the following example. The XML fragment shows a list of order IDs as a value of the OrderList attribute in the Employee element. If any of these values do not exist as the value of the OrderID attribute of the Order element, an error will be raised.

In a similar way, the EmployeeID attribute of the Order element is an idref attribute, referring to the EmployeeID attribute of the Employee element, thus forming an intra-document link. The id and idref relationships are similar to the primary key and foreign key relationships in relational databases.

  <Order OrderID="O1" OrderDate="1996-07-17T00:00:00" EmployeeID="Emp1" /> 
  <Order OrderID="O2" OrderDate="1996-08-01T00:00:00" EmployeeID="Emp1" /> 
  <Order OrderID="O3" OrderDate="1996-08-07T00:00:00" EmployeeID="Emp1" /> 
  <Order OrderID="O4" OrderDate="1996-08-20T00:00:00" EmployeeID="Emp2" /> 
  <Order OrderID="O5" OrderDate="1996-08-20T00:00:00" EmployeeID="Emp2" /> 
  ...
  <Employee EmployeeID="Emp1" 
            LastName="Davolio" 
            OrderList="O1 O2 O3">
  </Employee>
  <Employee EmployeeID="Emp2" 
            LastName="Fuller" 
            OrderList="O4 O5">
  </Employee>
  ...
  ...

Example C — Specifying the Data Type in an XML Document Instance

You can specify the data type of an element without requiring the use of a schema. To do this, you can use the dt:dt attribute or declare the data type as an element.

The following document instance shows how to specify the data type using the dt:dt attribute. In this example, the dt:dt attribute specifies the int data type for the Pages element.

<BookInfo xmlns:dt="urn:schemas-microsoft-com:datatypes">
  <Title>XML for Fertilizer</Title>
  <Pages dt:dt="int" >123</Pages>
</BookInfo>

Now, consider the following document instance. It uses the dt:dt attribute as in the preceding example. In this case, the attribute specifies the data type of the size 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>

However, you can rewrite the preceding instance so that you declare the data types as elements, as shown in the following example.

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

Following are some guidelines for working with data type elements:

  • The dataType property on data type elements returns the data type of the element. To return the typed value of the element, use the nodeTypedValue property. Using the nodeTypedValue property on an element that has no data type returns the string value of the element.
  • The parser validates the contents of data type elements against the type of the element. If the parser does not support a data type, it disregards that data type and no error occurs.
  • A data type element cannot have child elements.

Example D — Using Enumerations

In the following example, the schema specifies enumeration as the data type for an attribute.

<AttributeType name = "Attr1" 
             dt:type="enumeration" dt:values="a b c"/>

In MSXML 2.6 and later, elements also support the enumeration data type.

<ElementType name = "Ele1" 
             dt:type="enumeration" dt:values="a b c"/>

The following element is a valid instance for the preceding type declaration.

<Ele1>b</Ele1>

You can also specify the enumeration data type on an instance of the element. For example, the following element, Ele1, contains a type declaration and declares its values.

<Ele1 dt:dt="enumeration" dt:values="a b c" 
      xmlns:dt="urn:schemas-microsoft-com:datatypes">b</Ele1>

See Also

dataType Property | nodeTypedValue Property