Formatting XML with JSTL and XSLT

XML is becoming the unquestioned standard for data exchange, and it is now very common for JSP pages to receive XML data from some sort of a middle tier. Jeff Heaton discusses the ways JSP displays this XML data, and shows you how to use the tags provided by the JSP Standard Tag Library (JSTL).

From the author of

From the author of

Considerable data is now available in the form of XML. Web pages often need
to access XML data and display it. JSTL provides a range of XML tags that enable
you to perform a variety of operations on XML data. In this article, I will show
you how to process an XML and display it as HTML data. The first item needed for
this is an XML file. We will use the XML file shown in Listing 1 for this
purpose.

Tags are provided to allow you to iterate over XML data. You can also perform
comparisons on XML data using XPath expressions and access individual elements
of data within the XML document using XPath. This allows you to customize the
display of your XML using many of the JSTL tags you are already familiar
with.

Finally, the JSTL tag library enables you to process using XSL templates
(XSLT). By creating an XSL template, you can transform the XML data into HTML
output or even another XML document. (A complete discussion of XPath and XSLT is
beyond the scope of this article. For more information about these two
standards, you should refer to the W3C at
http://www.w3c.org.)

The XML tag library is broken up into three logical groups. The core tags
perform the basic parsing and access to individual elements. Flow control XML
tags enable you to iterate over element collections and perform logical
operations based on XPath expressions. Finally, transformation operations allow
you to use XSLT documents to reformat XML documents. We will examine all three
categories of tags in this chapter. But first, we must examine XPath, which is a
standard way of specifying sections of an XML document. The JSTL XML tag
libraries make extensive use of XPath.

Understanding XML Core Tags

Several core tags are provided by the JSTL XML tag library. These tags
perform very basic operations that are required by the other tags. The
<x:parse> tag is used to parse XML data. When the <x:parse> tag is
called, a variable is specified that the parsed XML document will be stored
into. For example, consider the following code:

This code begins by accessing the file http://www.site.com/file.xml,
which is loaded into the variable doc using the <c:import> tag. The
<c:import> tag allows the contents of a URL to be downloaded into a scoped
variable.

The contents of the downloaded XML file are then parsed using the
<x:parse> tag. The resulting document is stored in the scoped variable
doc.

Now that the document is parsed, we can display some of the values by using
XPath expressions, as previously mentioned. The XPath expressions are specified
as the select attribute that is passed to the <x:out> and <x:set>
tags. The XML document is accessed by specifying its scoped variable as part of
the XPath expression using the form "$doc".

You will find that many of the JSTL XML tags use this form. A select
attribute will be specified that holds an XPath expression that is to be
evaluated.

Now that we have seen how the core XML tags work in general, we will examine
each of these tags in detail. We will begin with the <x:parse> tag.