Home > Articles > Programming > Java

Formatting XML with JSTL and XSLT

Jeff Heaton
  • PrintPrint
  • Share ThisShare This
  • DiscussDiscuss
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).

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 ThisShare This
  • Your Account

Discussions

Make a New Comment

You must log in in order to post a comment.

Related Resources

Danny KalevMinutes from the October 2009 Meeting
By Danny Kalev on November 19, 2009 No Comments

The minutes from the Santa Cruz (October 2009) meeting are available here. Even if you're not a language layer at heart, I encourage you to read them.

Danny KalevA Reader's Opinion on Attributes
By Danny Kalev on October 20, 2009 No Comments

In August I dedicated a series to the debate about C++0x attributes. I believe that it covered the subject in a balanced and detailed way, but I keep getting complaints from C++ users who don't like attributes for various reasons. Here's a recent email I received from a Polish C++ programmer. While it  doesn't represent my opinion about attributes -- I'm rather neutral about this feature and consider it a "solution waiting for a problem" -- but it suggests that attributes are still a highly controversial issue that will haunt C++ for a long time. The email is quoted here with minor edits that and as usual, with all private details removed.

Danny KalevFollowup: The Web 2.0 Guy I Ain't
By Danny Kalev on October 16, 2009 1 Comment

Almost a year ago, I posted here The Web 2.0 Guy I Ain't. People wonder whether I still resist all those Web 2.0 features and technologies at the end of 2009.

See All Related Blogs

Informit Network