11.2. Tabular InformationThe previous chapter introduced several JavaScript class constructors in an effort to keep the client-side code manageable. Now is a good time to introduce another, a wrapper around the XSLT processor to handle the browser-specific details involving exactly what is required for XSL transformations. Displaying my usual lack of imagination, the class constructor is named appropriately enough: XSLTProcessor. Table 11-2 shows the properties and methods for this class.
With the creation of the XSLTProcessor constructor, the only items remaining are those that are absolutely essential to the website. The essential items are the XSL style sheets themselves, three in total. The first style sheet creates the HTML for the Items page. The purpose of the second style sheet is to create/render the Details page. The final style sheet renders the shopping cart in a slightly different manner than you'd expect. Each of these three items is covered as needed. 11.2.1. Read OnlyPlease bear with me; what I'm about to say deals only with read-only pages and, to some, might seem to be heresy. When using XSL for read-only pages, data binding isn't necessary; in fact, it is unnecessary overhead. Think about it for a moment: First, the information isn't going to change on the client side. In addition, the transformation process has already taken care of the display of the information. For the aforementioned reasons, it is perfectly acceptable to skip the bind when dealing with read-only information, as the style sheet in Listing 11-6 illustrates. Listing 11-6. XSL Style Sheet to Produce a Nonbound Table
This style sheet first creates an HTML Table element with the required attributes to give the site a common look and feel. Next, the column headers are rendered and a template is invoked to create the individual rows, which is the Table element in the source XML document. If there are no Table elements, only the HTML table headers will be produced. The individual cells are produced based upon the node name, and we're done. Before proceeding any further, however, I want to explain two statements in the style sheet. The first of these is the one that defines the apostrophe variable: <xsl:variable name="apostrophe">'</xsl:variable> The second statement is the one that uses the apostrophe variable: <xsl:value-of select="concat('javascript:pageLoad (',$apostrophe,'itemsDisplay.xsl',$apostrophe,', ',guild_id,',null)')" /> These two statements might seem somewhat odd because if you're even slightly familiar with XSL, you know that there is a perfectly acceptable entity that can be used to render apostrophes. The entity that I refer to is ', which, unfortunately, would cause quite a few headaches if used here. The entity would be treated as if it were, in fact, an apostrophe. The XSLT processor would then consider the previous statement to be equivalent to the following. <xsl:value-of select="concat('javascript:pageLoad (',','itemsDisplay.xsl',',',',guild_id,',null)')" /> As you can see, this would lead to an error and a nasty error message instead of the page shown in Figure 11-1. Figure 11-1. The properly rendered page![]() 11.2.2. UpdateableUnlike the previous read-only example, binding cannot be ignored when using XSLT to create updateable web pages. Even so, several advantages exist that were unavailable in earlier chapters. For example, there are the funky looping and concatenating strings to build the HTML with the correct number of rows. XSL takes care of those annoying details for us. |