Transforming XML into HTML

You may also have heard about the Extensible Stylesheet Language (XSL). XSL defines one set of XML elements to transform an XML document into some other type of document, and another set of elements to produce a formatted version of an XML document suitable for display. Browsers and other programs that need to render an XML document with different styles for different elements, such as a bold large font for a header and a regular font for paragraph text, use the formatting part of XSL. The transformation part of XSL is referred to as XSLT. XSLT can turn a source XML document, such as a document representing an order, into different forms using different style sheets. This is useful in business-to-business (B2B) applications, where different partners often require the same information in slightly different formats. You can read more about XSL and XSLT at

In a web application, XSLT can transform structured XML data into HTML. Example 14-2 shows an example of a JSP page in which the same phone book information used in Example 14-1 is transformed into an HTML table.

Example 14-2. Transforming XML to HTML (phone_html.jsp)

<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri="" %>
<%@ taglib prefix="x" uri="" %>
    <title>Phone List</title>
  <body bgcolor="white">
    <c:import url="htmltable.xsl" var="stylesheet" />
                   <x:transform xslt="${stylesheet}"> ...

Get JavaServer Pages, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.