O'Reilly logo

Java Servlet & JSP Cookbook by Bruce W. Perry

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

23.5. Using the XML Transform Tags

Problem

You want to use the JSTL's XML and XSLT-related tags.

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

A number of web site teams may already have devised stylesheets for transforming XML into HTML. In addition, you may want to separate most of the XML transformation responsibilities from JSPs, so that JSPs focus only on presenting the transformed information. The JSTL provides XML-related tags to easily integrate stylesheets into JSPs. Example 23-4 is an Extensible Stylesheet Language (XSL) document that converts an XML file into HTML. The stylesheet provides a conversion of an Ant build file similar to the one described in Recipe 23.3.

Example 23-4. The stylesheet for transforming an XML file

<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html"/> <xsl:template match="/"> <html><head><title>List of build.xml targets </title></head><body bgcolor="white"><h2>Build.xml targets</h2> <xsl:apply-templates /> </body></html> </xsl:template> <xsl:template match="/project"> <dl> <xsl:for-each select="./target"> <dt><b> <xsl:value-of select="@name" /></b>&#xA0;</dt> <xsl:if test="@depends"> <dd>depends=<xsl:value-of select="@depends" />&#xA0;</dd> </xsl:if> </xsl:for-each><!--end for-each ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required