RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Using XTags to Add XSLT Transformations to Your JSP Web Applications : Page 3

You don't have to write repetitive manual code to display database tables or apply transformations to display XML documents in your JSP Web applications; the Jakarta XTags tag library provides an easy way to apply an XSLT stylesheet to XML data.

Displaying Database Data
Here's how to retrieve and display the example Catalog database table in a JSP. Create a new JSP called DisplayData.jsp. In it, specify a taglib directive for the XTags tag library:

   <%@ taglib uri="http://jakarta.apache.org/taglibs/xtags-1.0" 
     prefix="xtags" %>
Import the XSU and DOM4J classes:

   <%@ page import=
      javax.naming.InitialContext" %>
Obtain a JDBC connection from the datasource JNDI "OracleDS" you configured for the WebLogic server:

   InitialContext ctx=new InitialContext();
   javax.sql.DataSource ds=(
   Connection connection=ds.getConnection();
Create an OracleXMLQuery object from the database table using a SQL query:

   OracleXMLQuery query = new OracleXMLQuery(
      connection, "SELECT CatalogId, " + 
Set the row tag for the XML document generated with XSU. This creates an XML document row tag for each row in the database table:

Obtain an org.w3c.dom.Document object from the OracleXMLQuery object:

   org.w3c.dom.Document domDocument=query.getXMLDOM();
Convert the org.w3c.dom.Document object to a org.dom4j.Document object:

   DOMReader domReader=new DOMReader();
   org.dom4j.Document dom4jDocument=domReader.read(domDocument);
Output the org.dom4j.Document object to an XML file:

   XMLWriter output=new XMLWriter(
      new FileWriter(
      "C:/BEA/weblogic81/samples/server/examples/" + 
Parse the XML document output with the XTags tag xtags:parse:

   <xtags:parse  uri="catalog.xml"/>
The XPath and XSLT tags in the JSP use the XML document parsed with the xtags:parse tag. Add an HTML table and a header row for the HTML table, and for each row in the XML document, add a corresponding HTML row to the table. You use the xtags:forEach tag to iterate over the "rows" in an XML document. The xtags:valueOf tag is used to obtain the value of an element in a XML document. Iterate over the XML document row tags and add rows to the HTML table.

Figure 1. Database Data Converted with XTags: Here's how the database table looks when displayed in a browser after the XTags conversion.
   <xtags:forEach  select="/ROWSET/CATALOG">
        <td><xtags:valueOf  select="CATALOGID"/></td>
        <td><xtags:valueOf  select="JOURNAL"/></td>
        <td><xtags:valueOf  select="PUBLISHER"/></td>
        <td><xtags:valueOf  select="EDITION"/></td>
        <td><xtags:valueOf  select="TITLE"/></td>
        <td><xtags:valueOf  select="AUTHOR"/></td>
Listing 1 shows the complete code in DisplayData.jsp.

Finally, copy the completed DisplayData.jsp file to the C:/BEA/weblogic81/samples/server/examples/build/mainWebApp directory. Now, if you browse to the file using the URL: http://localhost:7001/DisplayData.jsp, you'll see the database table displayed in the JSP as shown in Figure 1.

Once you get past the rather lengthy setup, you'll find that using the XTags tag library makes it much easier to use XSLT functionality in your JSPs.

Deepak Vohra is an O'Reilly technical reviewer, who reviewed the book WebLogic: The Definitive Guide. He's also a NuBean consultant and web developer, and is a Sun Certified Java 1.4 Programmer and Sun Certified Web Component Developer for J2EE.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date