AJAX - The Complete Reference

(avery) #1

164 Part I: Core Ideas


Given the bookmarks.xml file, we likely want to create an (X)HTML <table> to hold
the data for all the bookmarks. Each <bookmark> tag like

<bookmark class="favorite">
<title>Yahoo</title>
<url>http://www.yahoo.com</url>
<description>Thanks for the great Ajax library with docs even</description>
<rating>5</rating>
<lastvisit>March 1, 2007 3:05 PM</lastvisit>
<totalvisits>47</totalvisits>
</bookmark>

will be converted into a table row (<tr>) that looks something like the following markup:

<tr>
<td>
<a target="_blank" href=" Contents of <url> tag" >Contents of <title>
tag </a>
</td>
<td> Contents of <description> tag </td>
<td> Contents of <rating> tag </td>
<td> Contents of <lastvisit> tag </td>
<td> Contents of <totalvisits> tag </td>
</tr>

To accomplish this goal, employ the XSL file (http://ajaxref.com/ch4/bookmarks.xsl),
which first prints out the start of the bookmark table and the appropriate heading tags:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">

<table width='100%'><tbody id='mainbody'><tr><th>Bookmark</th>
<th>Description</th><th>Rating</th><th>Last Visit</th><th>Total Visits</th></tr>

Next, use XPath to select out each <bookmark> tag found within the <bookmarklist>
tag:

<xsl:for-each select="bookmarklist/bookmark">
<tr>
<td>
<a target="_blank"><xsl:attribute name="href">
<xsl:value-of select="url"/>
</xsl:attribute><xsl:value-of select="title"/></a>
</td>
<td><xsl:value-of select="description"/></td>
<td><xsl:value-of select="rating"/></td>
<td><xsl:value-of select="lastvisit"/></td>
<td><xsl:value-of select="totalvisits"/></td>
</tr>
</xsl:for-each>
Free download pdf