Home > Articles > Programming > Java

Formatting XML with JSTL and XSLT

  • Print
  • + Share This
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

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.

Listing 1: The Student XML File

<?xml version="1.0" encoding="ISO-8859-1"?>
<students>
  <student id="1">
   <name>
     <first>John</first>
     <last>Smith</last>
     <middle>T</middle>
   </name>
   <grade>
     <points>88</points>
     <letter>B</letter>
   </grade>
  </student>
  <student id="2">
   <name>
     <first>James</first>
     <last>Smith</last>
     <middle>K</middle>
   </name>
   <grade>
     <points>92</points>
     <letter>A</letter>
   </grade>
  </student>
  <student id="3">
   <name>
     <first>Kelly</first>
     <last>Lane</last>
     <middle>A</middle>
   </name>
   <grade>
     <points>72</points>
     <letter>C</letter>
   </grade>
  </student> 
</students>

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:

<!-- parse an XML document -->
<c:import url="http://www.site.com/file.xml" var="xml"/>
<x:parse source="${xml}" var="doc"/>
<!-- display using XPath expressions -->
<x:out select="$doc/name"/>
<!-- set a scoped variable -->
<x:set var="name" scope="request" select="$doc/name"/>

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.

  • + Share This
  • 🔖 Save To Your Account