devxlogo

Using XTags to Add XSLT Transformations to Your JSP Web Applications

Using XTags to Add XSLT Transformations to Your JSP Web Applications

oday’s applications often use XML technology to represent and transfer structured data. You can display the data in an XML document as HTML by applying an XSLT stylesheet transformation to the XML data. In a Java application, you perform the XML-to-HTML transformation by writing code that uses the javax.xml.transform package API. In a JSP Web application you can perform such transformations with a tag library called XTags that supports XSLT and XPath. Although you can use XML documents directly as a datasource, the preferred datasource for Web applications is a relational database (RDBMS). In a typical three-tier Web application the database provides the data storage, a middle tier application provides the conversion of database data to XML, and a client application such as a JSP or a JSF presents the data as HTML by applying an XSLT transformation.

In this article you’ll see how to develop a Web application that retrieves data from an Oracle database, converts it to XML with the Oracle XML SQL Utility (XSU), and presents the data as HTML via a JSP by applying an XSLT transformation. You can also apply CSS stylesheets in a JSP.

I’ve used WebLogic 8.1 as the application server, obtaining data via a JNDI datasource and a JDBC connection with the database, using the Apache Jakarta XTags tag library to perform the XSLT transformations. XTags supports both XSLT (transformation) and XPath (XML search/retrieval) within JSP pages. The example modifies the database table before displaying to the user as HTML.

Preliminary Setup
Follow these steps to prepare your development machine to run the example.

  1. Download and install WebLogic Server 8.1.
  2. Download and install Oracle 10g, including the sample schemas. Create an Oracle database instance, “OracleDB” for example. Create an example database table named Catalog, in the OE schema. Here’s an SQL script you can use to create and populate the table.
  3.    CREATE TABLE OE.Catalog(CatalogId VARCHAR(25),       Journal VARCHAR(25), Publisher Varchar(25),       Edition VARCHAR(25), Title Varchar(45), Author Varchar(25));      INSERT INTO Catalog VALUES('catalog1', 'Oracle Magazine',      'Oracle Publishing', 'March-April 2005',       'Starting with Oracle ADF', 'Steve Muench');      INSERT INTO Catalog VALUES('catalog2', 'Oracle Magazine',       'Oracle Publishing', 'March-April 2005',       'Aggregate Data with XQuery', 'Nilesh Junnarkar');      INSERT INTO Catalog VALUES('catalog3', 'Oracle Magazine',       'Oracle Publishing', 'July-August 2005',       'Tuning Undo Tablespace', 'Kimberly Floss');

  4. Download the Apache Jakarta XTags tag library and extract the zip file to a directory.
  5. XTags is based on the dom4j XPath framework. Download dom4j and extract the zip file to a directory.
  6. Download the Xalan XSLT implementation.
  7. Download XDK 10g.

To finish the setup, add the following files to your CLASSPATH where is the directory where you installed XDK 10g Production, and is the directory in which you installed Oracle Database 10g.

  • /lib/xsu12.jar
  • /lib/xmlparserv2.jar
  • /lib/xdb.jar
  • /LIB/servlet.jar
  • /jdbc/lib/classes12dms.jar

Configuring a DataSource in WebLogic
The Web application retrieves data from the Oracle database using a JDBC connection. You’ll need to configure a JNDI datasource in WebLogic to get the database connection to work. I’ve used the examplesServer included in the WebLogic server examples to deploy the web application. First, open the WebLogic server administration console by browsing to the URL http://localhost:7001/console. In the console, find the “Services > JDBC > Connection Pools” node, right-click on it, and then select “Configure a New JDBCConnectionPool” from the popup menu. In the “Choose database” frame select Oracle as the database type, and “BEA’s Oracle Driver (Type 4 XA)” as the database driver. Click on the Continue button.

To define connection properties for the database, specify the database instance you created earlier in this article as the Database Name, for example, “OracleDB.” Enter “localhost” as the Host Name, and “1521” as the Port. Specify “OE” as both the Database User Name and as the password. Click the Continue button.

In the “Test database connection” section, click on “Test Driver Configuration” to test the JDBC connection with the database. Click on the “Create and Deploy” button to create and deploy the connection pool. You use the connection pool to configure a datasource in JNDI by right-clicking on the Data Sources node and selecting “Configure a New JDBCTxDataSource.”

Specify a JNDI name for the datasource and click on the Continue button. WebLogic uses the datasource JNDI to obtain a JDBC connection for the JSP application. In the “Connect to Connection Pool” field, select the connection pool you configured earlier. Click on the Continue button. Select the examplesServer to deploy the datasource, and then click on the Create button to create and configure the datasource.

Integrating the Apache Jakarta XTags Tag Library
The application retrieves data from the datasource you configured in the previous section, converts the retrieved data to XML with the XML SQL Utility (XSU), then converts the XML to HTML using the Apache XTags tag library. To enable that to work, you need to add the jar files required for XSU and XTags to the WebLogic server’s classpath. Add the following jar files to the CLASSPATH variable that you’ll find in the C:BEAweblogic81samplesdomainsexamplesstartExamplesServer script file.

Table 1. Required JAR Files: The table shows a list of the jar files required to use XSU and XTags.

JAR FileDescription
dom4j-1.6.1.jarDOM4J
libjaxen-1.1-beta-6.jarJAXEN
xalan.jarXalan
xercesImpl.jarXerces
LIBxsu12.jarXML SQL Utility
LIBxmlparserv2.jarXML Parser
libxdb.jarOracle XDB
jdbclibclasses12dms.jarOracle JDBC
LIBservlet.jarServlet API

The XTags tag library provides several XSLT and XPath tags to transform an XML document in a JSP. Table 2 lists some of the common tags.

Table 2. Common XSLT and XPath XTags: The table shows some of the commonly used XTags.

XTags TagDescription
styleTransforms an XML document with an XSLT stylesheet.
paramSpecifies a parameter for XSLT transformation.
parseParses an XML document.
variableDefines a XSLT variable.
stylesheetThe xsl:stylesheet tag in a JSP.
templateThe xsl:template tag in a JSP.
applyTemplatesThe xsl:applyTemplates tag in a JSP.
elementThe xsl:element tag in a JSP.
attributeThe xsl:attribute tag in a JSP.
outputThe xsl:output tag in a JSP..

Next, configure the XTags tag library with the WebLogic server web application. In the C:BEAweblogic81samplesserverexamplesuildmainWebAppWEB-INF directory, you’ll find a web.xml file. Add a tag for the XTags tag library as follows:

                  http://jakarta.apache.org/taglibs/xtags-1.0                    /WEB-INF/xtags.tld         

Copy the C:XTagsjakarta-taglibsxtags aglibs-xtags.tld file to the C:BEAweblogic81samplesserverexamplesuildmainWebAppWEB-INF directory.

Create a lib directory in the WEB-INF directory. Copy the C:XTagsjakarta-taglibsxtags aglibs-xtags.jar file to the WEB-INF/lib directory.

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=      "oracle.xml.sql.query.OracleXMLQuery,       oracle.xml.sql.OracleXMLSQLException,      oracle.xml.sql.OracleXMLSQLNoRowsException,      org.dom4j.*,org.dom4j.io.*,      java.sql.*,      java.io.*,      org.w3c.dom.*,      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=(      javax.sql.DataSource)ctx.lookup("OracleDS");   Connection connection=ds.getConnection();

Create an OracleXMLQuery object from the database table using a SQL query:

   OracleXMLQuery query = new OracleXMLQuery(      connection, "SELECT CatalogId, " +       "JOURNAL, PUBLISHER, EDITION, TITLE,"  +       "AUTHOR FROM OE.CATALOG");

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:

   query.setRowTag("CATALOG");

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/" +       "build/mainWebApp/catalog.xml"));   output.write(dom4jDocument);

Parse the XML document output with the XTags tag xtags:parse:

   

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.
                                                                  

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.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist