FAQ
History |
![]() ![]() ![]() |
Search
Feedback |
XML Tags
A key aspect of dealing with XML documents is to be able to easily access their content. XPath, a W3C recommendation since 1999, provides an easy notation for specifying and selecting parts of an XML document. The JSTL XML tag set, listed in Table 6-4, is based on XPath.
Table 6-4 XML Tags Area Function Tags XML Coreout
parse
set
Flow Controlchoose
when
otherwise
forEach
if
Transformationtransform
param
The XML tags use XPath as a local expression language; XPath expressions are always specified using attribute
select
. This means that only values specified forselect
attributes are evaluated using the XPath expression language. All other attributes are evaluated using the rules associated with the global expression language.In addition to the standard XPath syntax, the JSTL XPath engine supports the following scopes to access Web application data within an XPath expression:
These scopes are defined in exactly the same way as their counterparts in the JSTL expression language discussed in Implicit Objects. Table 6-5 shows some examples of using the scopes.
The XML tags are illustrated in another version (
bookstore5
) of the Duke's Bookstore application. This version replaces the database with an XML representation (books.xml
) of the bookstore database. To build and install this version of the application, follow the directions in The Example JSP Pages replacingbookstore4
withbookstore5
.Since the XML tags require an XPath evaluator, which is provided in two libraries,
jaxen-full.jar
andsaxpath.jar
, included with the JSTL tag library.Core Tags
The core XML tags provide basic functionality to easily parse and access XML data.
The
parse
tag parses an XML document and saves the resulting object in the scoped attribute specified by attributevar
. Inbookstore5
, the XML document is parsed and saved to a context attribute inparseBooks.jsp
, which is included by all JSP pages that need access to the document:<c:if test="${applicationScope:booklist == null}" > <c:import url="/books.xml" var="xml" /> <x:parse xml="${xml}" var="booklist" scope="application" /> </c:if>The
out
andset
tags parallel the behavior described in Expression Tags for the XPath local expression language. Theout
tag evaluates an XPath expression on the current context node and outputs the result of the evaluation to the currentJspWriter
object.The
set
tag evaluates an XPath expression and sets the result into a JSP scoped attribute specified by attributevar
.The JSP page
bookdetails.jsp
selects a book element whoseid
attribute matches the request parameterbookId
and sets theabook
attribute. Theout
tag then selects the book's title element and outputs the result.<x:set var="abook" select="$applicationScope.booklist/ books/book[@id=$param:bookId]" /> <h2><x:out select="$abook/title"/></h2>As you have just seen,
x:set
stores an internal XML representation of a node retrieved using an XPath expression; it doesn't convert the selected node into aString
and store it. Thus,x:set
is primarily useful for storing parts of documents for later retrieval.If you want to store a
String
, you need to usex:out
withinc:set
. Thex:out
tag converts the node to aString
, andc:set>
then stores theString
as a scoped attribute. For example,bookdetails.jsp
stores a scoped attribute containing a book price, which is later fed into afmt
tag, as follows:<c:set var="price"> <x:out select="$abook/price"/> </c:set> <h4><fmt:message key="ItemPrice"/>: <fmt:formatNumber value="${price}" type="currency"/>The other option, which is more direct but requires that the user have more knowledge of XPath, is to coerce the node to a
String
manually using XPath'sstring
function.Flow Control Tags
The XML flow control tags parallel the behavior described in Flow Control Tags for the XPath expression language.
The JSP page
catalog.jsp
uses theforEach
tag to display all the books contained inbooklist
as follows:<x:forEach var="book" select="$applicationScope:booklist/books/*"> <tr> <c:set var="bookId"> <x:out select="$book/@id"/> </c:set>= <td bgcolor="#ffffaa"> <c:url var="url" value="/bookdetails" > <c:param name="bookId" value="${bookId}" /> <c:param name="Clear" value="0" /> </c:url> <a href="<c:out value='${url}'/>"> <strong><x:out select="$book/title"/> </strong></a></td> <td bgcolor="#ffffaa" rowspan=2> <c:set var="price"> <x:out select="$book/price"/> </c:set> <fmt:formatNumber value="${price}" type="currency"/> </td> <td bgcolor="#ffffaa" rowspan=2> <c:url var="url" value="/catalog" > <c:param name="Add" value="${bookId}" /> </c:url> <p><strong><a href="<c:out value='${url}'/>"> <fmt:message key="CartAdd"/> </a> </td> </tr> <tr> <td bgcolor="#ffffff"> <fmt:message key="By"/> <em> <x:out select="$book/firstname"/> <x:out select="$book/surname"/></em></td></tr> </x:forEach>Transformation Tags
The
transform
tag applies a transformation, specified by a XSLT stylesheet set by the attributexslt
, to an XML document, specified by the attributexml
. If thexml
attribute is not specified, the input XML document is read from the tag's body content.The
param
subtag can be used along withtransform
to set transformation parameters. The attributesname
andvalue
are used to specify the parameter. The value attribute is optional. If it is not specified the value is retrieved from the tag's body.
FAQ
History |
![]() ![]() ![]() |
Search
Feedback |
All of the material in The J2EE Tutorial for the Sun ONE Platform is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.