Skip navigation

Data Types in XML Data Schemas

As an XML developer, you'll want to choose a data-definition language that offers flexibility by letting you handle different data types. Although a Document Type Definition (DTD) lets you define each element attribute's name, a key advantage of an XML Data Schema (XDS) is that it lets you specify a data type for both elements and attributes. When you assign a data type to an attribute, you specify the data format and enable another level of validation and control for the XML parser or the client application that consumes the XML data through the XML Document Object Model (XMLDOM).

An XDS can contain two categories of data types. Along with the common data types that you find in all programming languages (string, date, integer), you can have any of the special data types included in the XML Language Specification, such as ID, IDREF, and NMTOKEN. A complete list of the XML data types is available on the Microsoft Developer Network (MSDN) in the XML Data Types Reference area. In addition, the XML 1.0 Recommendation from W3C—the consortium that governs XML technology—describes XML primitive types in Section 3.3.1.

If users access your application through Microsoft Internet Explorer (IE) 5.0 and later, an XDS needs to import the data type's (dt's) namespace to use the data type support. The schema declaration in the XML document would look like this:

<Schema name="myschema"
	xmlns="urn:schemas-microsoft-com:xml-data"
	xmlns:dt="urn:schemas-microsoft-com:datatypes">
<!— ... —>
</Schema>

In an XDS, attributes can appear in any order within an element, but each attribute can appear only once. Each attribute holds at least a name and a type:

<AttributeType name="description" dt:type="string" />

However, the full syntax for the <AttributeType> tag is the following:

<AttributeType
	default="default-value" 
	dt:type="primitive-type" 
	dt:values="enumerated-values"
	name="idref"
	required="\{yes | no\}" />

The "default" attribute specifies the default value for the attribute the <AttributeType> node describes. Attributes such as dt:type and dt:values belong to the dt namespace and complete the description of the data type required for that attribute. Usually, you need to specify only the dt:type, which can be a value such as string, boolean, date, number, int, float, time. The "name" attribute contains a string that the parser interprets as a reference to the attribute itself. The "required" attribute can take one of two possible values ("yes" or "no".) And you can specify whether the attribute can be omitted.

You can also choose enumerations as your data type. In this case, the syntax would look like this:

<AttributeType name="processed"
dt:type="enumeration"
dt:values="yes no" />

You need to list the possible values for the enumeration and separate each by a letter space. The Microsoft XMLDOM available with IE 5.0 lets you access the content of a typed attribute. You can read that value as normal text or as a typed value. You use the nodeValue property in the former case, and you use the nodeTypedValue property in the latter case.

TAGS: SQL
Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish