AJAX - The Complete Reference

(avery) #1

Chapter 4: Data Formats 167


PART I


Before concluding this section, it is important to point out one complexity that has been
introduced—fetching two dependent files (bookmarks.xml and bookmarks.xsl). You applied
simple sequencing to keep things straight, but you might wonder: why not request each file
in parallel? You could have done that, but then you would have had to write code to
address the possibility that the responses might arrive out of order. You will see in Chapter
6 that making multiple requests can provide numerous opportunities for trouble and
potentially add more complexity to your Ajax applications than you might have initially
imagined.

NNOT EOTE Similar to the situation with XPath, XSLT 2 was made a W3C recommendation in early
2007 (www.w3.org/TR/xslt20/). However, given the state of browser support, we focus on the
older version.

Data Islands: Proprietary and Powerful

Like it or not, Internet Explorer has numerous proprietary features, but be careful not to
dismiss all these browser-specific ideas as bad ideas. Some are quite powerful and elegant,
and because of this have worked their way into common use. In fact, the XMLHttpRequest
object could certainly be counted as just such a feature. Inspired by the effort required in the
previous example, we discuss two more IE-introduced features that are being rediscovered
by Ajax-focused Web developers: data islands and data binding.
Since Internet Explorer 4, it has been possible to associate tables and other HTML
structures with data sources. The data sources might be remote, or they can be XML found
within a document. In Internet Explorer a proprietary <xml> tag can be used to hold the
XML content and thus is dubbed a data island. Why the odd name? Likely the namer of this
feature considers this XML information to be but a small island found within a sea of
structural or (unfortunately more likely) presentational markup. Moving beyond the origins
of its name, however, let’s bind the bookmarks.xml file to an <xml> tag-based data island.
You don’t need to hide it with CSS but you will do so in case other browsers are around.

<xml id="xmlBookmarks" style="display:none;"
src="http://ajaxref.com/ch4/bookmarks.xml"></xml>

Later in the document, you set the proprietary datasrc attribute to point to the data
island. You could point it to the remote file directly, but this is more appropriate since it will
lead to a cross-browser style shortly.
Within the table, you set <span> and <a> tags to pull the contents of the individuals
tags out by setting their datafld attributes to the names of the corresponding XML tags.

<table width="100%" datasrc="#xmlBookmarks">
<tr>
<td width="15%"><a datafld="url" href="#" target="_blank">
<span datafld="title"></span></a>
</td>
<td width="35%"><span datafld="description"></span></td>
<td width="10%"><span datafld="rating"></span></td>
<td width="25%"><span datafld="lastVisit"></span></td>
<td width="15%"><span datafld="totalVisits"></span></td>
</tr>
</table>
Free download pdf