[ Team LiB ] Previous Section Next Section

XML Tag Library

JSTL includes a number of tags for processing and formatting XML. These tags make it much easier to develop XML-based Web applications.

XML Core Tags

The XML core tags focus on three basic capabilities: parsing an XML document, storing value from an XML document into a named variable, and displaying data stored in an XML document.

XPath Expressions in JSTL Tags

Because the JSTL XML tags store parsed XML data in JSP variables, you need a way to extract values out of these variables. You usually use XPath to extract data from XML files (technically, XPath simply specifies where in the document the data is located), but for JSTL you need an easy way to associate an XPath expression with a particular variable. JSTL defines a number of XPath scopes that can be combined with XPath expressions. In a simple case, you can refer to a variable containing a parsed document by using the form $varname where varname is the name of the variable. For example, if you parse an XML document into a variable named doc and you want to refer to an element named services directly under the root element named configuration, use this XPath expression: $doc/configuration/services.

You can also specify a scope in a variable name, like this: $scopenameScope:varname. For example, to access a variable called data in the session scope, use the expression $sessionScope:data.

You can also access form parameters, header values, cookie values, and servlet init parameters with the expressions $param:paramName, $header:headerValueName, $cookie:cookieValueName, and $initParam:initParamName. Unlike the scoped variables, which may be any Java type, these four types of values are always strings. Unfortunately, they can't be embedded in the middle of XPath expressions. For example, suppose you want to create an XPath expression that locates all nodes in the variable doc whose name matches a form parameter called which. You might think that the expression $doc//$param:which would work, but it doesn't. Instead, you must use a more verbose form: $doc//.[name()=$param:which]. In this longer form, you basically select descendant nodes by using the name function, which returns the node name.

The <xml:parse> Tag

The <xml:parse> tag parses an XML document from a string variable, from a reader, or from the body of the tag itself. Use the xml attribute to specify the location of the parsed XML. The XML can be a string, an EL expression that evaluates to a string, or an EL expression that evaluates to a reader (like the result of <c:import>).

If you don't specify an xml attribute, then the <xml:parse> tag parses its own body content (that is, the XML content should be contained within the <xml:parse> tag).

Like other JSTL tags, the <xml:parse> tag supports the var and scope attributes to specify the variable where the parsed document will be stored. The actual type of object stored in the variable is implementation-dependent. It might be a Document object, but it might be something else. If you specifically want to parse a document and store it in a DOM Document object, use the varDom and scopeDom attributes, rather than var and scope.

The <xml:set> Tag

The <xml:set> tag extracts a value from an XML document and stores the value in a variable. The select attribute specifies an XPath expression for the value to be extracted, whereas the var and scope attributes specify the variable that receives the extracted value.

The <xml:out> Tag

The <xml:out> tag extracts a value from an XML document and writes the value to the JSP's writer (similar to the way that <c:out> displays values). The select attribute specifies an XPath expression for the value to be displayed. If the result of an expression contains XML tags and you want to display the tags on a browser, set the escapeXML attribute to true, which causes characters such as < to be written as &lt, so that they will be displayed as is by the browser.

Listing 19.15 shows a JSP whose <xml:parse> tag parses its own body content and displays a value selected by a form parameter called which.

Listing 19.15 Source Code for xml1.jsp
<%@ taglib prefix="xml" uri="http://java.sun.com/jstl/xml" %>
<html>
<body>
<xml:parse var="parsedXML">
<?xml version="1.0"?>
   <doc>
      <message>Hello!</message>
      <thing1>I am thing1</thing1>
      <thing2>I am thing2</thing2>
   </doc>
</xml:parse>
<xml:out select="$parsedXML//.[(name()=$param:which]"/>
</body>
</html>

You can run this example by passing form parameters in the URL. For example, with a typical Tomcat installation, the URL would be http://localhost/tyjsp/ch19/xml1.jsp?what=This+is+a+test.

XML Flow Tags

The XML flow tags—<xml:if>, <xml:choose>, and <xml:forEach>—work just like their <c:if>, <c:choose>, and <c:forEach> counterparts, except that instead of specifying EL expression values with the value tag, you specify them with an XPath expression in the select tag.

XML Transform Tags

The <xml:transform> tag applies an XSLT stylesheet to an XML document and stores the results in a variable. The xml attribute should contain an EL expression that evaluates to either a string containing the document or a reader that reads the document. If you don't specify the xml attribute, the <xml:transform> tag performs the XSLT transform on its own body content. The xslt attribute specifies the XSLT stylesheet to apply and must contain an EL expression that evaluates to a string containing the stylesheet, a reader that reads the stylesheet, or an XSLT Transform object. The var and scope attributes specify a variable that will contain the results of the transform, which can then be processed with the other XML tags.

    [ Team LiB ] Previous Section Next Section