Oracle JSP - Developers Guide
Release 1.0


Prev Next

7
XML/XSL and JSPs

XML and XSL are technology standards that are quickly gaining popularity in the web application developers market. The two technologies, used together, allow one to cleanly separate an applications data from the rules or logic that transform that data into a particular presentation. XML is the standard used to represent the data. XSL is the standard used to represent the transformation rules.

As XML/XSL allows one to dynamically generate HTML (or other MIME-type based formats), one might wonder how JSPs can play a role in this technology (or vice versa). JSPs empower the XML/XSL developer in two ways:

Many applications are more easily implemented if they maintain server-side state. Though these applications may use XML/XSL to generate their presentation, JSPs provide the application structure and management from which these presentations are controlled.

Doing most of the above is straightforward. It does involve knowing the Java Servlet/JSP programming models, but as neither of these are restricted to generating HTML, you use them identically to generate XML or XSL. However, there is one mismatch in the technologies. Many uses of XML/XSL on the server involve applying the XSL transformation on the server before returning the results to the client. JSPs do not support a notion of a final transformation or filtering step. Rather all its generated output is written directly into the response stream. To support XSL transformation of dynamically generated XML in a JSP page, you must first write special JSP stream code that would temporarily replace its output stream. Additionally, you would have to write the code to setup and call your XSL engine to transform the data passed through your special JSP stream.

Oracle JSP defines a JML tag to simplify this common programming style. This chapter discusses how to use this tag to support XSL transformations within a JSP page. Samples that illustrate the concepts discussed in this chapter are provided in the chapter 7 samples section included in the download.

7.1 <jml:transform>

The <jml:transform> tag or its synonym <jml:stylesheet> is used to apply an XSL stylesheet to a portion of the JSP output before the result is returned to the client.

7.1.1 Syntax

The tag's syntax is:

<jml:transform href="xslRef" >

The body of the tag contains regular JSP/static text that together
generate the XML the stylesheet is applied to.

</jml:transform>

<jml:stylesheet> is a supported synonym for <jml:transform>.

The href can refer to either a static XSL stylesheet or a dynamically generated one. That is the target of the href can be a .jsp or servlet that provides the requested stylesheet.

The href can be either JSP local or fully qualified. A JSP local reference follows the same conventions as the urlSpec in the <jsp:include> and <jsp:forward> tag. A reference that begins with a "/" is located relative to the application's root. A reference that does not begin with a "/", is located relative to the directory containing this page. A fully qualified href is one that specifies the protocol and server in the reference. For example: http://www.w3c.org/xml/spec.xml

The href itself can be dynamically specified. By default, the value of href is a static String. However, you can use either the standard JSP attribute "<%= expression %>" syntax or the JML attribute "$expression" syntax to provide a dynamically computed value.

7.1.2 Use

As with all JML tags, to use the <jml:transform> tag, it must first be declared via the <%@ taglib %> directive.

<%@ taglib uri="oracle.jsp.parse.OpenJspRegisterLib" prefix="jml" %>

This directive must reside either in the page itself, or in the globals.jsa.

Typically, the <jml:transform> tag is used to transform the entire page. However, because its syntax only applies the transformation stylesheet to the body of its tag, you can have distinct blocks in your JSP page. Each block is bounded by its own <jml:transform> tag with its own href stylesheet reference. Likewise, though less common, the <jml:transform> tag can be nested. For example, you can use the <jml:transform> tag within the body of a <jml:transform> tag.

7.1.3 Sample

The following is a basic sample that uses <jml:transform> to transform static XML. The code for the JSP page is contained in hello.jsp:

hello.jsp

<%@ page session = "false" %>
<%@ taglib uri="oracle.jsp.parse.OpenJspRegisterLib" prefix="jml" %>

<jml:transform href="hello.xsl" >

<page>
<title>Hello</title>
<content>
<paragraph>This is my first XML/XSL file!</paragraph>
</content>
</page>

</jml:transform>

The body of the <jml:transform> tag is processed using the stylesheet hello.xsl:

hello.xsl

<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="page">
<html>
<head>
<title>
<xsl:value-of select="title"/>
</title>
</head>
<body bgcolor="#ffffff">
<xsl:apply-templates/>
</body>
</html>
</xsl:template>

<xsl:template match="title">
<h1 align="center">
<xsl:apply-templates/>
</h1>
</xsl:template>

<xsl:template match="paragraph">
<p align="center">
<i>
<xsl:apply-templates/>
</i>
</p>
</xsl:template>

</xsl:stylesheet>

This generates the following HTML output:

<root>
<html>
<head>
<title>Hello</title>
</head>
<body bgcolor="#ffffff">
<h1 align="center">Hello</h1>

<p align="center"><i>This is my first XML/XSL file!</i></p>

</body>
</html>
</root>

Which is displayed in the browser as:


Prev

Next

Oracle
Copyright © 1999 Oracle Corporation.

All Rights Reserved.