Recipe 23.4 Using the XML Core JSTL Tags


Recipe 23.4 Using the XML Core JSTL Tags

Problem

You want to use the JSTL's XML tags in a JSP.

Solution

Use the various XML tags after declaring the tag library with the proper taglib directive ( uri attribute of http://java.sun.com/jstl/xml for JSTL 1.0 or http://java.sun.com/jsp/jstl/xml for JSTL 1.1).

Discussion

Many web developers have to write programs that parse or read XML to find information, or they have to write code that displays the encapsulated XML information in a readable format. The JSTL XML tags are a nice tool for these tasks . Example 23-3 displays some information from an Ant build.xml file. (See Chapter 4 on the Ant tool if you are new to Ant.) I'm using this XML file just for an example of how to use the XML- related JSTL tags. Notice that the taglib directives at the top of the page allow the use of the XML and core JSTL tags further along in the code.

Example 23-3. A JSP parses an ant build file
  <%@ taglib uri="http://java.sun.com/jstl/xml" prefix="x" %> <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>  <html> <head><title>Using the Core XML tags</title></head> <body> <h2>Here are the target and property values from the XML file</h2>  <c:import url="http://localhost:8080/home/build.xml" var="buildXml" /> <x:parse xml="${buildXml}" var="antDoc" />  <h3>First the target names...</h3>  <x:forEach select="$antDoc/project/target" >       <x:out select="@name"/>      <x:if select="@depends"> : depends=<x:out select="@depends"/></x:if><br />   </x:forEach>  <h3>Then property names and values...</h3>  <x:forEach select="$antDoc/project/target/property" >       <x:out select="@name"/>: value= <x:out select="@value"/><br />   </x:forEach>  </body> </html> 

Example 23-3 uses the c:import tag to import a build file and store it in a variable called buildXml . The the x:parse tag then parses the imported document into a form or object that the other XML tags can work with. The code stores the parsing result in another variable named antDoc .

The XML JSTL tags use some of the same tag names as the core library, but a different prefix ("x"). The x:forEach tag in Example 23-3 uses an XPath expression as the value of the x:forEach select attribute.

XPath is an XML technology that is designed to search for and select portions or "node sets" of the hierarchical tree represented by an XML document. While an XPath tutorial is well beyond the scope of this recipe (it is like a little programming language in itself), there are plenty of online tutorials and books on the subject. You can start at the Sun Microsystems web services tutorial, which includes a discussion of XPath: http://java.sun.com/ webservices /docs/1.3/tutorial/doc/.


The x:forEach tag makes nested elements such as x:out available to any nodes grabbed by the XPath expression. The code in Example 23-3 displays the name of each Ant target in the build.xml file by first collecting a set of all of the target elements with this expression:

 <%-- this XPath expression is the equivalent of "begin at the root 'project' element  and get all of its nested 'target' elements" --%> <x:forEach select="$antDoc/project/target" > 

Example 23-3 outputs the name of each target with this code:

 <x:out select="@name"/> 

The enclosing x:forEach tag establishes the context of the XPath expression in this x:out tag.


The following code states "return true if the current node has a valid depends attribute."

 <x:if select="@depends"> 

If that expression returns true , the nested x:out tag outputs the value of the depends attribute. Figure 23-2 shows the result of requesting the JSP of Example 23-3 in a browser. I've converted the XML information into a more readable format for a browser. XML mavens (meaning those who can sift through all of these crazy acronyms!) declare that you can also use Extensible Stylesheet Language Transformations (XSLT) for converting XML information to HTML or other readable forms. This is the next recipe's topic.

Figure 23-2. A JSP shows the output of a parsed XML file
figs/jsjc_2302.gif

See Also

The Jakarta Project's Taglibs site: http://jakarta.apache.org/ taglibs /index.html; Sun Microsystem's JSTL information page: http://java.sun.com/products/jsp/jstl/; Recipe 23.3 on using the core tags; Recipe 23.5 on using the XML Transform tags; Recipe 23.6 on using the formatting tags; Recipe 23.7 and Recipe 23.8 on the JSTL's SQL features; Recipe 23.9-Recipe 23.14 on using the EL to access scoped variables , cookies, and JavaBean properties.



Java Servlet & JSP Cookbook
Java Servlet & JSP Cookbook
ISBN: 0596005725
EAN: 2147483647
Year: 2004
Pages: 326

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net