The XML tags provide a way to easily access and manipulate the content of an XML document for JSPs. The custom tags for XML use XPath expressions to perform their actions.
JSTL provides actions that allow a developer to parse and search for data in an XML document that meets certain search criteria.
The parse action parses the content of the XML and stores the contents in a reference that can be used in the JSP. The source can be specified by either the source attribute or the body of the parse tag. The var attribute specifies the JSP scoped attribute in which to save the result. For example, the JSP below parses the items element and stores it in a variable named catalog.
<%@ taglib uri="http://java.sun.com/jstl/xml" prefix="x" %> <!-other JSP code-> <x:parse var="catalog"> <items> <item> <quantity>3</quantity> <productnumber>229AXH</productnumber> <description>High speed photocopier machine with automatic sensors </description> <unitcost>1939.99</unitcost> </item> <item> <quantity>1</quantity> <productnumber>1632</productnumber> <description>One box of color toner cartridges</description> <unitcost>43.95</unitcost> </item> </items> </x:parse>
The example below shows how a JSP may parse the XML output from another JSP and apply an org.xml.sax.XMLFilter, using the filter attribute:
<x:parse filter="${olditems}"> <jsp:include page="xmloutputter.jsp"/> </x:parse>
Another example shows how a JSP can read an XML file containing a catalog, parse it, and store it in the application context, so that it can be used across the application. The example also shows how XML tags can be combined with other JSTL tags:
<%@ taglib uri="/jstl-c" prefix="c" %> <%@ taglib uri="/jstl-x" prefix="x" %> <c:if test="${empty applicationScope.xmlcatalog}" > <c:import url="/items.xml" var="xmlfile" /> <x:parse xml="${xmlfile}" varDom="xmlcatalogDOM" scope="application" /> </c:if>
The parse action does not perform any validation against DTD or schemas. If the var is used to store the processed content, it does not require a specific mechanism. However, the varDOM variable requires that the object be represented as a org.w3c.dom.Document object.
The out action enables the developer to denote an XPath expression when working with XML documents. The expression specified is applied to the current node, and the result is sent to the JspWriter object for that page. For example, the tag below references the street element in the billingaddress and prints it to the output stream:
<%@ taglib uri="http://java.sun.com/jstl/xml" prefix="x" %> <!-other JSP code-> <x:out select="${billingaddress/street}" />
The action is analogous to the JSP expression <%= BillingAddress.getStreet() %>.The out tag can also specify if the special entity characters, such as "<", "&", or "," must be converted to their corresponding entity codes (<, &, etc.) by including an escapeXml attribute:
<x:out select="${billingaddress/street}" escapeXml="true" />
The set action is similar to the out action, but rather than sending the result of the XPath evaluation to the output stream, it stores the results in a variable with a specified scope:
<%@ taglib uri="http://java.sun.com/jstl/xml" prefix="x" %> <!-other JSP code-> <x:set var="streetaddr" select="${billingaddress/street}" scope="session"/>
If the scope is not specified, the default scope of page is assigned to the variable.
JSTL includes actions to help iterate over elements in an XML document as well as conditionally process JSP code fragments, depending on the result of an XPath expression.
The forEach action allows developers to iterate over a collection of XML elements that can be specified by the select attribute. The select attribute must contain a valid XPath statement (i.e., XSL statement), which filters the XML. For example, the following JSP segment can be used to iterate over the items element shown earlier:
<x:forEach var="item" select="$applicationScope:catalog/items/*"> Item quantity: <x:out select="$item/quantity"/> Item product number: <x:out select="$item/productnumber"/> Item cost:$ <x:out="$item/unitcost"/> </x:forEach>
The if action is a conditional operator that includes a select attribute. The body of this tag will execute only when the XPath statement in the select attribute evaluates to true at runtime. For example, the following JSP prints out the description if the price is more than $1000:
<x:forEach var="item" select="$sessionScope: catalog/items/*"> <x:if select="$item/unitcost >1000"> This item has a cost greater than $1000: <x:out select="$item/description"/> </x:if> </x:forEach>
The choose action acts like a switch statement in Java and, when it evaluates to true, executes its body. The choose action can include multiple when actions that act like the case statement in Java. For example, the choose action can take the following form:
<x:choose> body content with <x:when> and <x:otherwise> sub tags </x:choose>
The when action is a child of the when tag and has a select attribute. The select attribute contains an XPath expression and, when that evaluates to true, the body is executed. For example, the code below can be included in an iteration loop and will execute the two different when blocks in the choose tag:
<x:choose> <x:when select="$item/unitcost >1000"> This item has a cost greater than $1000: $ <x:out select="$item/unitcost"/> <br> <x:out select="$item/description"/> </x:when> <x:when select="$item/unitcost < 100"> This item has a cost less than $100: $ <x:out select="$item/unitcost"/> <br> <x:out select="$item/description"/> </x:when> </x:choose>
The otherwise tag is another child of the choose tag. It is similar to an else statement, in that its body is executed only when all the preceding when actions evaluate to false. For example, the code below can be included in an iteration look and will execute the otherwise block only if the when condition is not satisfied:
<x:choose> <x:when select="$item/unitcost < 10"> This item has a cost less than $10: $ <x:out select="$item/unitcost"/> <br> <x:out select="$item/description"/> </x:when> <x:otherwise> This item is more than $10 <x:out select="$item/description"/> <br> </x:otherwise> </x:choose>
One of the most common uses of XML in a JSP is to present different views of the same data by applying different style sheets.
The transform action applies an XSL transformation to an XML document. It can apply the style sheet to either an XML source or the body of the tag itself. The output can be saved in a variable var, which represents an instance of an org.w3c.dom.Document class, or can be stored in a javax.xml.transform.Result object. For example, the tag below applies an XSL to an XML, and the result is sent to the output stream:
<x:transform xml="xslinput.xml" xslt="fluteadmin.xsl"/>
The following tag applies the transformation and stores the result as a DOM object in the session:
<x:transform xml="xslinput.xml" xslt="fluteadmin.xsl" result="mydom" scope="sessionScope"/>
This example applies the transformation to the body:
<x:transform" xslt="fluteadmin.xsl" result="mydom" scope="sessionScope"> some xml structure here </transform>