Online Book Reader

Home Category

AJAX In Action [234]

By Root 4025 0
tree.

Building the XSLT document

Building the XSLT transformation for this project is rather simple. Since we are developing a table, we won’t need any special pattern matching; instead, we will loop through the source tree element nodes. The template that we’ll develop outputs an HTML table with four columns. Listing 12.6 shows the XSLT file for this project.

Listing 12.6 XSLT file

b Set XML version and encoding

xmlns:xsl=

"http://www.w3.org/1999/XSL/Transform"> c

Specify XSLT namespace

d

Set template rule

e

Add table element

f Create

heading

row

select="phonebook/entry"> g

Loop through phonebook entries

entry data

CompanyContactCountryPhone
select="company"/>h Output the

select="contact"/>

select="country"/>select="phone"/>

When we create an XSLT transformation, we need to state the version and encoding b of the XML. The XSLT namespace c needs to be specified. The namespace gives the document the rules and specifications that it is expected to follow. The Licensed to jonathan zheng

Combining the XSLT and XML documents

481

elements in the XML namespace are recognized in the source document and not in the results document. The prefix xsl is used to define all our elements in the XSLT namespace. We can then set the template rule to the match pattern of / d, which references the whole document.

We can start building the table template that displays our results. We add the table tag e, giving the table an ID. The table header row f is next inserted, which contains the column names to be displayed to the user so she can understand what information is contained in the table. By looping through the source node set, we obtain the remaining rows of the table. For this, we use the for-each loop g to iterate over the records to obtain the nodes that are located in phonebook/entry.

The column values have to be selected as we are looping through the document tree. To select the values from the nodes, we use value-of h, which extracts the value of an XML element and adds it to the output stream of the transformation. To specify the XML element whose text we want to retrieve, we use the select attribute with the element’s name. Now that we have built the XSLT file and created the code to dynamically generate the XML document, we can finish building the JavaScript code to see how the XSLT transformation structures our XML file into a viewable table when we combine them.

The next step takes us back to the client, which retrieves the files that we just created with the HTTP response.

12.4 Combining the XSLT and XML documents

Back on the client, we need to combine the XSLT and XML documents from the server. When using an XSLT transformation, we’ll find that the browsers differ on how they combine the two documents. Therefore, we first check to see what method the browser supports in order to load and combine our two documents. Again we’re using the ContentLoader object, introduced in chapter 3. It is contained in the external JavaScript file, net.js. This file does all of the work of determining how to send the information to the server, hiding any browserspecific code behind the easy-to-use wrapper object.

Now we can begin the process of obtaining the server-side files and combining them on the client. In listing 12.7, the LoadXMLXSLTDoc() function is being called from the function GrabNumber() in listing 12.2. The function GrabNumber() passes in the values for the URL that generates the XML data, the XSL file, and Licensed to jonathan zheng

482

CHAPTER 12

Live search using XSLT

the output reference

Return Main Page Previous Page Next Page

®Online Book Reader