Before you start
The objective of this tutorial is two-fold -- to showcase the Ajax/Web 2.0 approach to Web development, and to showcase the strengths of DB2 9 pureXML™. With this in mind, I wrote the Information On Demand 2006 Scheduler application, in conjunction with the Information On Demand 2006 Global Conference. It is a simple application for planning your conference itinerary. It takes advantage of the Ajax -- Asynchronous JavaScript and XML -- approach to Web programming to make user interaction as intuitive and painless as possible. It also takes advantage of the speedy pureXML capabilities of IBM DB2 9.
Figure 1 illustrates the development of the IOD Planner application. All loading is done in the background to avoid the refresh lag, and rich widgets are used for form fields, dialog boxes, and drop-down menus.
Figure 1. IOD Planner 2006
You should be reasonably familiar with Web development and databases. The tutorial "DB2 XML evaluation guide" (developerWorks, June 2006) gives an excellent introduction to DB2 9, while "Query DB2 XML Data with XQuery" (developerWorks, April 2006) does the same for XQuery.
To deploy the source code, you will need a Windows or Linux operating system. On it, you will need Zend Core for IBM (see Resources for a download link). Zend Core conveniently bundles the DB2 9 Express-C data server, Apache2 Web server, PHP scripting language, DB2 extensions for PHP, and a powerful administration console.
Figure 2. Traditional Web model and Ajax model
The key tenet of the Ajax philosophy is to avoid loading new Web pages. Instead, you load new content in the background and then show it within the existing page. This avoids the wasteful reloading of basic infrastructure and the frustration of waiting for the browser to render the same navigation and design. Your users never see a blank, loading screen. The final effect is that of a speedy, very responsive application.
Requests to the server are sent using an XMLHTTPRequest object, and responses are
handled asynchronously as they come in. Contrary to the name, they are not limited to XML. Any plain text format, from JavaScript (JSON) to CSV to pre-generated HTML, can be received by this mechanism. We chose the latter one -- HTML generated on the server.
Major Ajax-style Web applications include Bloglines, Del.icio.us, Digg, Flickr, Google Maps, Gmail, and Reddit.
Why Dojo JavaScript Framework?
In the past two years, JavaScript has matured a great deal as a language and as a platform. The language does not have one official library, framework, or API, so several industry-supported and open-source alternatives have appeared. They offer easy Ajax, rich widgets, data structures, helper functions, effects, and layout assistance. One of these toolkits is Dojo.
Dojo is an open source JavaScript framework backed by IBM, Sun, AOL, JotSpot, and others. Ajax for IBM WebSphere® ships Dojo 0.4.1. Dojo isn't tied to any specific server-side toolkit -- meaning it can be used with PHP, Java servlets, .NET, or any other back end. There is an active user and developer community, with hundreds of daily messages in the dojo-interest mailing list.
Dojo has a modular design, with extra functionality loaded on demand.
Figure 3. JavaScript framework adoption (Ajaxian.com, 2006)
As the number of contenders in Figure 3 suggests, Dojo is a new field. It's a quickly consolidating one. Both Dojo and Script.aculo.us made large mindshare gains in the past year. Of the two, Dojo has a more robust architecture and greater industry backing.
A relational database back end for a Web application is a given. We've chosen DB2 Express-C, which is a free-to-download, free-to-deploy, and free-to-distribute version of the DB2 9 data server. It also has native XML capabilities, which calls for some elaboration.
Figure 4. Historical ways of combining XML with a relational database
Any sufficiently large enterprise application tends to have XML in it. Often, XML is the most natural form for the data to take. This means that XML is either stored in files, stored wholesale in database CLOBs, or shredded into distinct columns.
Alas, mixing individual XML files with a relational database is hard to scale; XML in CLOBs is hard to query and slow; shredding XML into relational columns means that the relational schemas have to be synchronized with XML schemas -- every change to XML structure triggers expensive regression testing on the whole database.
Figure 5. Native XML in DB2
DB2 9 solves the problem. Its native XML data type is easy to query with SQL and W3C-standard XQuery. It scales with the database, and XML-specific optimizations give it better performance than CLOBs. Finally, it keeps XML and relational schemas separate -- there's no need to regression test everything when you've only changed the XML structure.
Figure 6. XQuery and DB2
XQuery is a W3C standard for querying data from XML files. The WHERE clauses in XQuery and SQL are similar, and the FOR loop is intuitive to casual programmers. XQuery code is faster to write, easier to read, and generally friendlier than equivalent XSL.
FLWOR ("flower") is often mentioned in the context of
XQuery. It refers to FOR, LET,
WHERE, ORDER BY, and RETURN statements, the foundations of XQuery.
For an excellent introduction, please consult the article"Query DB2 XML Data with XQuery."
PHP is a popular and easy-to-learn server-side scripting language. It has a lower learning curve than Java servlets or .NET, while offering the same powerful, advanced features for the experienced professional. As an interpreted scripting language, it is very friendly to iterative development -- there's no recompiling after a single change, and you can see the impact of your code changes in real time.
Needless to say, you should have separate development, testing, and production environments. Live editing is not recommended.
We chose XSL for its powerful transformations. In hindsight, it would have been better to rely exclusively on XQuery. Its facilities are also excellent, and our code would have been shorter and more elegant. XQuery is a friendlier, side-effect-free language tailored for transforming from one XML structure to another. We are already using XQuery to generate well-formed XML, so we could have bypassed the second transformation entirely.





