background image

Core Tags

<< XML Tag Library | Flow Control Tags >>
<< XML Tag Library | Flow Control Tags >>

Core Tags

TABLE 7­5
Example XPath Expressions
XPath Expression
Result
$sessionScope:profile
The session-scoped EL variable named profile
$initParam:mycom.productId
The String value of the mycom.productId context parameter
The XML tags are illustrated in another version (bookstore5) of the Duke's Bookstore
application. This version replaces the database with an XML representation of the bookstore
database, which is retrieved from another web application. The directions for building and
deploying this version of the application are in
"The Example JSP Document" on page 185
.
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 EL variable
specified by attribute var. In bookstore5, the XML document is parsed and saved to a context
attribute in
tut-install/javaeetutorial5/examples/web/bookstore5/web/books/parsebooks.jsp,
which is included by all JSP pages that need access to the document:
<c:if test=
"${applicationScope:booklist == null}" >
<c:import url=
"${initParam.booksURL}" var="xml" />
<x:parse doc=
"${xml}" var="booklist" scope="application" />
</c:if>
The set and out tags parallel the behavior described in
"Variable Support Tags" on page 205
and
"Miscellaneous Tags" on page 211
for the XPath local expression language. The set tag
evaluates an XPath expression and sets the result into a JSP EL variable specified by attribute
var
. The out tag evaluates an XPath expression on the current context node and outputs the
result of the evaluation to the current JspWriter object.
The JSP page
tut-install/javaeetutorial5/examples/web/bookstore4/web/books/bookdetails.jsp
selects a book element whose id attribute matches the request parameter bookId and sets the
abook
attribute. The out 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 a String and store it. Thus, x:set is
primarily useful for storing parts of documents for later retrieval.
XML Tag Library
Chapter 7 · JavaServer Pages Standard Tag Library
213