The XML JSTL tag library
provides an easy way to parse XML documents and to do Extensible
Stylesheet Language Transformations (XSLT). This tag library uses XPath
expressions to navigate through elements in an XML document.
XPath is an
expression language used for finding information in an XML document, or
for making calculations based on the content of an XML document. For
more information about XPath, refer to http://www.w3.org/TR/xpath.
The following example illustrates the most commonly used tags in the XML JSTL tag library:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:import url="customers.xml" var="xml" />
<x:parse doc="${xml}" var="doc" />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>XML Tag Demo</title>
</head>
<body>
<table cellpadding="0" cellspacing="0" border="1">
<tr>
<td>First Name</td>
<td>Last Name</td>
<td>Email</td>
</tr>
<x:forEach select="$doc/customers/customer">
<tr>
<td>
<x:out select="firstName" />
</td>
<td>
<x:out select="lastName" />
</td>
<td>
<x:choose>
<x:when select="email">
<x:out select="email" />
</x:when>
<x:otherwise>
<c:out value="N/A" />
</x:otherwise>
</x:choose>
</td>
</tr>
</x:forEach>
</table>
</body>
</html>
The first thing we should notice in this example is the use of the<c:import>
core JSTL tag to import an XML file from a URL. The value of the URL
attribute defines the URL where the XML file can be located; it can be a
relative or absolute URL. In the example, the customers.xml file is in the same directory as the JSP, therefore a relative path is used to obtain it. The customers.xml file has customer information including first name, last name, and email, as shown next:
<?xml version="1.0" encoding="UTF-8"?>
<customers>
<customer>
<firstName>Karl</firstName>
<lastName>Smith</lastName>
<email>karls@nonexistent.org</email>
</customer>
<customer>
<firstName>Jenny</firstName>
<lastName>Conte</lastName>
<email>jenny@notreal.com</email>
</customer>
<customer>
<firstName>Rhonda</firstName>
<lastName>Benedict</lastName>
</customer>
</customers>
After packaging the previous two files in a WAR file and visiting the JSP's URL, we should see a page like the following:
The first JSTL XML tag we see in the example is the<x:parse> tag. This tag parses an XML document and stores it in the variable defined by its var attribute. The XML document to parse is defined in its doc attribute.
The XML JSTL tag library
contains several tags that are analogous to similar tags in the Core
JSTL tag libraries. These tags include<x:if>, <x:choose>, <x:when>, <x:otherwise>, <x:forEach>, <x:param>, and<x:set>. Usage of these tags is very similar to their core tag counterparts. The main difference is that these tags contain a select attribute containing an XPath expression to evaluate, instead of the value attribute that the corresponding core tags contain. The example illustrates the usage of most of these tags.
The next JSTL XML tag we see in the example is the<x:forEach>
tag. This tag iterates over the elements of an XML document. Elements
to iterate over are specified as an XPath expression through the select attribute.
The next JSTL XML tag we see in the example is the<x:out> tag, which outputs the value of the XPath expression defined in its select attribute.
Next, we see the<x:choose> tag, which is the parent tag of the<x:when> and (optionally)<x:otherwise> tags. The body of the first nested<x:when> tag containing an XPath expression evaluating to true as its select attribute is executed. select expressions for subsequent<x:when> attributes are not evaluated after one of them evaluates to true. If no select attributes for any of the<x:when> tags evaluate to true, the body of the optional<x:otherwise> tag is executed.
An additional XML JSTL tag is the<x:transform> tag, which is used to do XSLT transformations on XML documents. This tag is typically used with two attributes. The xml attribute indicates the location of the XML document to transform. It can be imported via the<c:import> tag, as illustrated in the example. The xslt attribute indicates the XSL stylesheet used to transform the document. This stylesheet can also be imported via the<c:import> tag.
The following table lists all of the JSTL XML tags:
Tag
|
Description
|
Example
|
---|
<x:choose>
|
Used to wrap the<x:when> and (optionally)<x:otherwise> tags. The body of the first<x:when> tag containing a select expression that evaluates to true is executed. If none of the<x:when> tags contain a test expression that evaluates to true, then the body of the<x:otherwise> tag is executed.
|
See example for<x:forEach>.
|
<x:forEach>
|
Iterates over the elements of an XML document. The elements to iterate over are specified through the select attribute.
|
<x:forEach select= "$doc/customers/customer">
<tr>
<td>
<x:out select="firstName" />
</td>
<td>
<x:out select="lastName" />
</td>
<td>
<x:choose>
<x:when select="email">
<x:out select="email" />
</x:when>
<x:otherwise>
<c:out value="N/A" />
</x:otherwise>
</x:choose>
</td>
</tr>
</x:forEach>
|
<x:otherwise>
|
Its body gets executed if none of the test expressions in the<x:when> tags nested in the same<x:choose> tag evaluate to true.
|
See example for<x:forEach>.
|
<x:out>
|
Outputs an XPath expression defined by the select attribute.
|
See example for<x:forEach>.
|
<x:param>
|
Adds a parameter to the containing<x:transform> tag.
|
See example for<x:transform>.
|
<x:parse>
|
Parses an XML document and stores it in the variable defined by its var attribute.
|
<x:parse doc="${xml}" var="doc" />
|
<x:set>
|
Saves the result of the XPath expression defined in its select attribute into a variable in the specified scope. If no scope is defined, a default scope of page is used.
|
<x:set var="custEmail" select="email"/>
|
<x:transform>
|
Transforms the XML document defined by the xml attribute using the XSL stylesheet defined by the xslt attribute.
|
<x:transform xml="${someXmlDoc}" xslt="${xslt}">
<x:param name="paramName" value="${paramValue}"/>
</x:transform>
|
<x:when>
|
Its body gets executed when its select expression evaluates to true.
|
See example for<x:forEach>.
|