Transforming XML with XSLT
Using the flow control and core XML tags of JSTL, you can transform XML documents into nicely formatted HTML documents. JSTL supports another standard that makes it even easier to format XML. Using XSL templates (XSLT) you can specify the exact format that you want your XML document to be formatted into. This format is specified by using an XSLT file.
A complete discussion of XSLT is beyond the scope of this article. If you are interested in learning XSLT, you can visit the following Web site: http://www.w3.org/TR/xslt. You may also find the following book useful: Sams Teach Yourself XSLT in 21 Days, by Michiel van Otegem (Sams, 2002, ISBN: 0672323184).
Our discussion of XSLT is limited primarily to the way XSLT is integrated with JSTL. JSTL allows you to specify two input files and receive the output. The input files are an XML data file and an XSLT formatting file. The output will be what was specified using the XSLT formatting file. A properly written XSLT format file can output data in nearly any text format. The two most common forms of files to output from an XSLT file are HTML and XML. XSLT is commonly used to generate HTML output from XML data; XSLT is also commonly used to translate between two incompatible XML formats.
The JSTL XML tags provide two tags that are used to work with XSLT files. The primary tag is the <x:transform> tag that actually does the transformation. The second tag provided by the JSTL XML tag library is the <x:param> tag, which allows you to specify parameters that will be passed to the XSLT script. These script parameters allow you to customize the operation of your XSLT script file.
Listing 1 shows the XML file that we used for all examples in this article. We now look at an example of how to use XSLT with this example file. To do this, we need to use an XSLT file that file must be designed to work with the students.xml file. The XSLT file that we will be using is shown in Listing 2.
Listing 2: A Sample XSLT File
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="students"> <html> <head> <title>XSLT Transform</title> </head> <body> <table border="1"> <tr><th>First</th><th>Last</th> <th>Points</th><th>Letter</th></tr> <xsl:apply-templates/> </table> </body> </html> </xsl:template> <xsl:template match="student"> <tr> <td><xsl:value-of select="name/first"/></td> <td><xsl:value-of select="name/last"/></td> <td><xsl:value-of select="grade/points"/></td> <td><xsl:value-of select="grade/letter"/></td> </tr> </xsl:template> </xsl:stylesheet>
Listing 2 presents an XSLT file that will format the XML file shown in Listing 1 to a readable HTML format. We will now examine the basic structure of this file. The XSLT file in this example is responsible for the generation of the entire HTML document. Everything from the beginning <html> tag to the ending </html> tag is generated by this document. This XSL file uses templates. Templates are provided that handle specific blocks of the XML document. The node that encloses the entire student file is the node named students, and an XSL template is provided that encapsulates this node. Because the students node is the root node, it will be the first template executed. The root template is shown here:
As you can see, this template specifies that it seeks a match on the value "students". This template then goes on to set up the header information and begin the table, as follows:
<html> <head> <title>XSLT Transform</title> </head> <body> <table border="1"> <tr><th>First</th> <th>Last</th> <th>Points</th> <th>Letter</th> </tr>
Now that we have reached the body of the table, we will use the XSLT tag <xsl:apply-templates>. This means to process the child nodes of the current tag. In this case, it means that all student records will be processed:
<xsl:apply-templates/> </table> </body> </html> </xsl:template>
For each student record encountered, a matching student <xsl:template> will be sought. The following shows just such a template. For each student record encountered, the following template will be executed:
<xsl:template match="student"> <tr>
To display each of the attributes for the student, the <xsl:value-of> tag will be used. Here, you can see the <xsl:value-of> tag will be provided relative XPath expressions that specify the element that is to be displayed. The following lines display the student attributes:
<td><xsl:value-of select="name/first"/></td> <td><xsl:value-of select="name/last"/></td> <td><xsl:value-of select="grade/points"/></td> <td><xsl:value-of select="grade/letter"/></td> </tr>
Now that you have seen how to construct a basic XSLT document, we will see how this document can be used with the JSTL XML tags to create HTML. We will begin by examining the <x:transform> tag.