Java / JSP / JSTL

Index

JSTL XML Library

XML Tags

XML is becoming increasingly important to page authors, and the JSTL provides XML actions that address the basic needs of those developers. The XML actions can be divided into core, control flow, and transformation actions. The XML core actions are similar to those provided in the core actions discussed above, and include <x:out>, <x:set>, and <x:parse>. The main difference between the core actions discussed above and the XML core actions is that XML actions support XPath expressions, while the core actions do not. As a matter of fact, the XML actions are based on XPath expressions. If you're unfamiliar with XPath, it's a language for defining parts of an XML document; XPath uses path expressions to identify nodes in an XML document.

The XML control flow actions are the same as the core actions discussed above. They include: <x:if>, <x:choose>, <x:when>, <x:otherwise>, and <x:forEach>. The main difference is that you use the select attribute to specify XPath expressions. Here is an example:

<x:forEach select="$output/portfolio/stock">
  <x:out select="price"/>
</x:forEach>

Now, let's look at a set of examples that demonstrate the tags discussed so far. In Code Sample 1, xml-ex1.jsp uses the core and XML tag libraries to process an XML document. In this example, the XML document is embedded within the page and the <x:parse> tag is used to parse the document. Then, an XPath expression is used to select items from the document.

Code Sample 1: xml-ex1.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jstl/xml" %>

<html>
<head>
  <title>JSTL Support for XML</title>
</head>
<body bgcolor="#FFFFCC">
<h3>Books Info:</h3>

<c:set var="xmltext">
  <books>
    <book>
      <title>Book Title A</title>
      <author>A. B. C.</author>
      <price>17.95</price>
    </book>
    <book>
      <title>Book Title B</title>
      <author>X. Y. Z.</author>
      <price>24.99</price>
    </book>
  </books>

</c:set>

<x:parse xml="${xmltext}" var="output"/>
<b>The title of the first book is</b>: <x:out select="$output/books/book[1]/title"/>
<br>
<b>The price of the second book</b>: <x:out select="$output/books/book[2]/price"/>
</body>
</html>

If you request xml-ex1.jsp, you'll see something like Figure 1.

figure 1
Figure 1: JSTL support for XML

In the above example, the XML text is included within the JSP page. In a real application, however, the XML document would be hosted on a local or remote web server. You can import a remote resource by using the <c:import> tag. Consider the XML document shown in Code Sample 2, which is located at http://www.javacourses.com/stocks.xml.

Note: If you'd like to see how JSP pages can be used (and the amount of code involved) in processing stocks.xml, take a look at Web Application Development with JSP pages and XML.

Code Sample 2: stocks.xml

<?xml version="1.0" encoding="UTF-8"?>
<portfolio>
  <stock>
    <symbol>SUNW</symbol>
    <name>Sun Microsystems</name>
    <price>17.1</price>
  </stock>
  <stock>
    <symbol>AOL</symbol>
    <name>America Online</name>
    <price>51.05</price>
  </stock>
  <stock>
    <symbol>IBM</symbol>
    <name>International Business Machines</name>
    <price>116.10</price>
  </stock>
  <stock>
    <symbol>MOT</symbol>
    <name>Motorola</name>
    <price>15.20</price>
  </stock>
</portfolio>

This document can be imported using the following snippet of code:

<c:import url="http://www.javacourses.com/stocks.xml" var="xmldoc"/>

XML Conditional Logic

The conditional logic in the XML tag library is the same as the one in the core tag library. As an example, the following snippet of code parses an XML document, then selects (using an XPath expression) the stock with the symbol SUNW.

<x:parse xml="${xmldoc}" var="output"/>
<x:if select="$output/portfolio/stock[symbol = 'SUNW']">
   You still have Microsystems Stocks!
</x:if>

if/then/else

You can implement an if/then/else clause using the <x:choose> action, which selects one among a number of possible alternatives. It consists of a sequence of <x:when> elements, followed by an optional <x:otherwise>. Each <x:when> element has a single attribute, select, that specifies an XPath expression. When a <x:choose> element is processed, each of the <x:when> elements has its expressions evaluated in turn. Only the body of the first <x:when> element (whose result is true) is rendered. If none of the test conditions of nested tags evaluates to true, the body of the <x:otherwise> tag is evaluated, if present.

Here is an example:

<x:parse xml="${xmldoc}" var="output"/>

<x:choose>
  <x:when select="$output/portfolio/stock[price > '70']">
    You still have stocks worth over $70.
  </x:when>
  <x:otherwise>
    You have no stocks worth over $70.
  </x:otherwise>
</x:choose>

Iteration

The <x:forEach> action evaluates the given XPath expression, and iterates over the result. The example shown in Code Sample 3, xml-ex2.jsp, shows how to print stock information from an XML document.

Code Sample 3: xml-ex2.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jstl/xml" %>

<html>
<head>
  <title>JSTL Support for XML</title>
</head>
<body bgcolor="#FFFFCC">
<h3>Portfolio</h3>

<c:import url="http://www.javacourses.com/stocks.xml" var="xmldoc"/>
<x:parse xml="${xmldoc}" var="output"/>

<p>

<table border="2" width="50%">
 <tr>
 <th>Stock Symbol</th>
 <th>Company Name</th>
 <th>Price</th>
 </tr>
 <tr>

<x:forEach select="$output/portfolio/stock" var="item">
  <td><x:out select="symbol"/></td>
  <td><x:out select="name"/></td>
  <td><x:out select="price"/></td></tr>
</x:forEach>
</table>

</body>
</html>

When you request this page, you will see something similar to Figure 2.

figure 2
Figure 2: Importing and parsing an XML document, then iterating over its nodes

Transforming XML

Many web applications use XSLT stylesheets to transform XML within JSP pages, and the JSTL supports this. The XML document to be transformed, as well as the XSLT stylesheet to be used, can be retrieved from a relative, absolute, or remote URL. The result of the transformation is written to the page. As an example, the stocks.xml document shown in Code Sample 2 (which is located at a remote web server) is transformed using the XSLT stylesheet (stocks.xsl) shown in Code Sample 4 but fetched from a local web server:

Code Sample 4: stocks.xsl

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl=
"http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method="html" indent="yes"/>

<xsl:template match="/">
  <html>
  <body>
   <xsl:apply-templates/>
  </body>
  </html>
</xsl:template>

<xsl:template match="portfolio">
  <table border="2" width="50%">
    <xsl:for-each select="stock">
      <tr>
        <td>
          <i><xsl:value-of select="symbol"/></i>
        </td>
        <td>
          <xsl:value-of select="name"/>
        </td>
        <td>
          <xsl:value-of select="price"/>
        </td>
      </tr>
    </xsl:for-each>
  </table>
</xsl:template>
</xsl:stylesheet>

The following example, xml-ex3.jsp (shown in Code Sample 5) demonstrates how to transform an XML document located on a remote web server using the stocks.xsl stylesheet shown in Code Sample 4.

Code Sample 5: xml-ex3.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jstl/xml" %>

<html>
<head>
  <title>JSTL: Importing XML Document</title>
</head>
<body bgcolor="#FFFFCC">
<h3>Transforming stocks.xml into HTML using stocks.xsl</h3>

<c:import url="http://www.javacourses.com/stocks.xml" var="xmldocument"/>
<c:import url="./stocks.xsl" var="xslt"/>
<x:transform xml="${xmldocument}" xslt="${xslt}"/>

</body>
</html>

When you load xml-ex3.jsp, you should see something in the browser similar to Figure 3.

figure 3
Figure 3: Transforming an XML document using an XSLT stylesheet

Conclusion

The JavaServer Pages Standard Tag Library (JSTL) satisfies the demand for a set of standard JSP custom tags to handle common tasks needed by all JSP pages, including conditional processing, XML processing, internationalization, and database access. As you can see from this article, JSTL is easy to work with, and its reusable standard custom tags lead to faster development of web applications.

Now that you know the basics of the JSTL, you can explore the rest of the tag libraries on your own. Have fun!



Next Lesson - Functions




© Copyright 2004 - ARABX Pty Ltd. All Rights Reserved