| [ directory ] |
|
8.4 Processing XML in JSPsThe standard tag library provides a number of tags that make it easy and natural to move through XML documents using XPath. An example of these tags in action is shown in Listing 8.5. Listing 8.5 Using XPath expressions in a JSP
<%@ taglib prefix="x"
uri="http://java.sun.com/jstl/xml" %>
<%@ taglib prefix="c"
uri="http://java.sun.com/jstl/core" %>
<c:import
url="http://localhost:8080/jspbook/chapter08/
collection.jsp"
var="xml"/>
<x:parse xml="${xml}" var="doc"/>
Albums by <x:out select="$doc//artist[1]/@name"/>:<br>
<ul>
<x:forEach select="$doc//artist[1]/album" var="album">
<li><x:out select="$album/@name"/>
</x:forEach>
</ul>
First, note that this example loads a new portion of the standard tag library, which is imported with the prefix x. The first new tag used in this example, c:import, is not technically a part of the XML tags but is often used in conjunction with them. The tag c:import works like a superenhanced version of the jsp:include tag. Amazingly, c:import can grab data from anywhere, not only from the site where the page lives. This makes it possible for sites to include content from other sites, although in general this should be done only with the other site's knowledge and permission. This ability works especially well in conjunction with XML, as will soon be demonstrated. The c:import tag stores the data it has read in a variable rather than automatically sending it to the user. This makes it possible to process this data before the user sees it, which is what will be done here. In this case, the data from the collection page from Listing 8.1 has been put into a variable called xml. This data could then be shown directly to the user with a simple <c:out value="${xml}"/>. Rather than display this data, it is instead passed to another tag, x:parse, the first of the new XML tags. This tag takes a block of XML and processes it internally into a form that can be used more efficiently. The results of this conversion are stored in yet another variable, which has been called doc. Next, data is extracted from this internal representation with the x:out tag. This tag works somewhat like c:out but obtains the value to display from a combination of the expression language and an XPath expression. The JSP XML tags allow the beginning of a select expression to start with a number of expression language identifiers, such as the variable doc that was created with the x:parse tag. Immediately following that can be any valid XPath expression, which will be used to pull data from the variable. Here, the pages gets the name of the first artist in the collection. Next is an x:forEach tag, which is to c:forEach what x:out is to c:out. The x:forEach tag will repeat some action for every element returned by an XPath expression, which in this case is all albums from the first artist. As with c:forEach, each time through the loop, the current value can be assigned to a variable, in this case one called album. Within the body of the x:forEach tag is another x:out, which displays the value of the name attribute for each album. Because album holds each of the XML album tags, the XPath portion of this second x:out tag does not need the full path starting from the top but instead needs to know only how to get to the name attribute from each album tag. Note that it would also have been possible to write this loop as
<x:forEach select="$doc//artist[1]/album/@name"
var="name">
<li><x:out select="$name"/>
</x:forEach>
This loop would have the effect of looping over all album names instead of over all albums. This works, as all the page will be showing is the name, but if it had to show both the name and the year the album was released, the page would have had to loop over the albums and then use two x:out tags to display the two different attributes. The x:if, x:choose, x:when, and x:otherwise tags do essentially the same things as their counterparts from the c library, except that each can take an XPath expression instead of a value from the expression language. This functionality was covered in Chapter 4 and so will not be repeated here. |
| [ directory ] |
|