jqjacobs.net/web/xml

XML Data Binding

Database Basics  |  Data Islands  |  Binding Elements

Introduction

Data binding refers to mapping, synchronizing, and transporting data from a source to an end-user document, and often involves data manipulation, filtering, sorting, or searching on the local system.  A variety of data sources can provide information, everything from embedded data to simple text files, XML documents, and complex relational databases.  Appropriate applications retrieve, synchronize, and present the information.  This reading focuses on XML data binding, one method of accomplishing this task.

One advantage of data binding is client-side data manipulation, which reduces client-server communication, network traffic, and server workload, and can increase client efficiency.  Another advantage is the separation of data maintenance tasks from presentation and formatting tasks.  Separation of data and presentation allows use of a dataset in diverse formats and different end-user documents, and it allows data binding from multiple sources in a single presentation document.  An advantage of using XML to store data is the document type of XML documents; many different applications read plain text, a non-proprietary standard.

With XML data binding, there is a the cross-platform compatibility issue.  A distinct disadvantage of XML data binding, especially with regard to WWW use, is the lack of universal support by browsers.  Microsoft Internet Explorer (IE) versions 5.0 + support XML data binding.  Netscape 6 supports XML, but not XML data binding.  XML data binding is especially suitable for a closed Microsoft environment, such as an intranet.  For an Internet site, data binding is viable given sufficient resources to implement multiple and complementary data access schemes for the full spectrum of client software.

Separator line

Database Basics

A typical database consists of one or more recordsets (often termed tables).  Database recordsets consist of records, collections of related data about a single entity, such as a person.  Fields are the different categories of information about a record, such as name, address, etc.  Data can be entered in fields as text or with data-type formatting, such as dates, currency, numbers, and logical operators (yes/no).  Fields contain single items of data.  XML data storage follows these fundamentals.

In XML, recordsets are classified as either simple and hierarchical.  Simple recordsets have a single root element, each record contains the same fields, and each field contains character data.  Hierarchical recordsets contain collections of nested recordsets.  The number of nestings is unlimited.

Separator line

Data Islands

XML data binding requires attaching a Web page to a recordset, called a data island.  The data can be located in external XML files or internally coded.  An external data island conforms to the recommended practice of separation of data and presentation.  The unofficial HTML element xml is used to embed XML in HTML documents.  Data islands are created by assigning an ID attribute to the xml tag.  The first example below creates a data island using an external XML file in the same folder.  The source src attribute specifies the data file location.

<xml id="customers" src="customers.xml>

The following example creates an internal data island in a simplified HTML document.  The requisite XML declaration and root element are included in the example.

<html> <body> <xml id="customers"> <?xml version="1.0"?>
   <customer>
      <name>Font Factory</name>
      <name>BeBop Grafix</name>
     </customer>
</xml> </body> </html>

IE's XML parser creates a data island from an XML document by storing the data as a Data Source Object (DSO).  Interactions between data islands and the Web page are controlled by the DSO.  The data is sorted or filtered without additional server requests.  After downloading, the data is static for the session, and updating the data requires a server request.

Separator line

Binding XML Elements to Tags

The HTML 4.0 specification reserves certain attributes for possible future use.  Four of these, datasrc, datafld, dataformatas, and datapagesize, are employed by IE data binding.  These element attributes allow IE to display bound data.  The datasrc attribute specifies the data island source with a URI.  The datafld attribute specifies elments in the XML data source.  The dataformatas attribute converts the default text property to HTML.  The datapagesize attribute specifies the number of records to display in tables binding multiple records.  The syntax used to employ these attributes follows.

The datasrc attribute links an HTML element to a DSO.  The attribute property specifing the unique identifier of a DSO must be prefixed by a number sign (#).  The datafld attributes reference specific elements in the XML data source.  The following syntax is required:

<tag datasrc="#id" datafld="field_name">

The datasrc attributte can be used with the following elements: a, applet, button, div, frame, iframe, img, label, marquee, select, span, table, textarea, and the input tags (types button, checkbox, hidden, image, password, radio, and text).

Not all HTML tags support data binding.  And some use the values of data in different ways. The span tag binds the value of the bound element in the field, while the anchor (a) tag replaces the href attribute property with the value of the bound element and, by default, treats the value as text.  The dataformatas attribute solves this problem with support for HTML code stored in XML elements.  The dataformatas attribute is only supported by the button, div, label, marquee, and span tags.  The syntax of the dataformatas attribute follows:

dataformatas="type" where the attribute property types are text or html

While most HTML elements only support displaying a single record, the table element supports display of multiple records.  A table can be bound to a DSO.  IE uses the table row element, tr, as a template, and the template repeats the row until all records are displayed.  The following syntax example binds three data fields from a recordset in three different cells.  Because the table data, td, element does not support data binding, tags that supports data binding need to be enclosed in the table cells.  This example employs the span element.

<table datasrc="#id"><tr>
<td><span datafld="field_x"></span></td>
<td><span datafld="field_y"></span></td>
<td><span datafld="field_z"></span></td></tr> </table>

Table data binding will display all records, with each record displayed in a different table row.  Portions of a recordset are displayed with a technique called paging.  Table pages are created by adding the datapagesize attribute to the table element, followed by an integer property specifying the number of records to display.  The datapagesize attribute is used to restrict the number of records displayed.  The following syntax is used:

<table datasrc="#id" datapagesize="integer">

Binding several levels of nested recordsets requires matching levels of nested tables.

Warning:  XML element attributes are treated as fields by the IE DSO.  When an attribute is associated with a field element, the field content can become disassociated from the field while the attribute is treated as the data.  Avoid using attributes in field elements in XML recordsets used for data binding.

Separator line

References and Advanced Topics

MSDN Library > Introduction to Data Binding Tutorial

Home
Classes
Web Design
XML
Contact

2004 by James Q. Jacobs. All rights reserved.

jqjacobs.net/web/xml