caucho
Resin
FAQ
Reference Guide
Demo
Tutorial

JSP page
Config
URLs
Database Forms
XTP Copy
Hello Tag
Vary Filter
HardCore
Mailing Forms
Beans
Cache
XSL Filter
run-at

Attr Tag
Mail Tag
XSL Tag
Connection Tag
 XSL Tag

Mail Tag
Hello Tag
Connection Tag

An XSL tag can take advantage of Resin's XSL API to transform the body of a tag using a stylesheet. Although it's significantly less efficient than XTP, this kind of tag still has useful applications.

Using the XSL tag

The XSL tag applies the stylesheet to the tag contents. The stylesheet will convert meaningful content tags into HTML formatting.

Because the content and the style are separated, it will now be easy to change the stylesheet for a different format.

xsl.jsp
<%@ taglib prefix="ct" uri="/WEB-INF/taglib.tld" %>
<%
 String author = "John Doe";
 String title = "A Guide to XSL";
%>

<ct:xsl stylesheet="tag.xsl">
<item>
  <author><%= author %></author>
  <title><%= title %></title>
</item>
</ct:xsl>

Author: <b>John Doe</b><br>
Title: <em>A Guide to XSL</em><br>

The following stylesheet uses StyleScript, Resin's readable syntax for XSL. You can, of course, convert this example to use strict XSL.

This example needs to explicitly set the output method to HTML so the <br> tag will be printed properly. Using normal XML rules, that tag would be printed as <br/>.

tag.xsl
$output(method=>html);

item <<
  Author: $apply-templates(author);<br/>
  Title: $apply-templates(title);<br/>
>>

author <<
<b>$apply-templates();</b>
>>

title <<
<em>$apply-templates();</em>
>>

Implementation of the XSL Tag

The implementation transforms a string to another string. The XSL API itself can transform many input formats to many output formats, but well just use the easiest one.

The stylesheet is compiled to a Java class for performance, and the StylesheetFactory is intelligent enough to load an already-compiled class to avoid overhead.

  1. Set the stylesheet search path.
  2. Create the StyleScript factory.
  3. Parse the stylesheet.
  4. Create a transformer.
  5. Transform the string.

One twist that's thrown into this example is the use of MergePath. MergePath lets you combine several directories into a single search path and treat the whole combination as a single Path object. We'll use that do duplicate the search path used by the XTP and XSL-Filter servlets.

XslTag.java
package test;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

import com.caucho.transform.*;
import com.caucho.vfs.*;
import com.caucho.xsl.*;

public class XslTag extends BodyTagSupport {
  private String stylesheet = "default.xsl";

  public void setStylesheet(String stylesheet)
  {
    this.stylesheet = stylesheet;
  }
  
  public int doEndTag()
    throws JspException
  {
    HttpServletRequest req;
    req = (HttpServletRequest) pageContext.getRequest();
    
    Path pwd = Vfs.lookup(req.getRealPath("/"));
    
    ServletContext app = pageContext.getServletContext();

    Path appDir = Vfs.lookup(app.getRealPath("/WEB-INF/xsl"));

    // search pwd, then appDir, then the classpath/>
    MergePath mergePath = new MergePath();
    mergePath.addMergePath(pwd);
    mergePath.addMergePath(appDir);
    mergePath.addClassPath(getClass().getClassLoader());
      
    try {
      // use StyleScript
      StylesheetFactory factory = new StyleScript();

      // set the search path
      factory.setStylePath(mergePath);

      // create the stylesheet
      Stylesheet xsl = factory.newStylesheet(stylesheet);

      StringTransformer transformer = xsl.newStringTransformer();

      String body = getBodyContent().getString();

      // transform the body
      String value = transformer.transformString(body);

      JspWriter out = pageContext.getOut();

      out.print(value);
    } catch (Exception e) {
      throw new JspException(e.toString());
    }

    return SKIP_PAGE;
  }
}

Summary

  • Althought XTP is more efficient, you can use inline XSL tranformations.
  • StyleScript is a readable syntax for XSL.
  • MergePath creates search paths with the Path API.
  • StringTransformer transforms strings to strings.

Mail Tag
Hello Tag
Connection Tag
Copyright © 1998-2002 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.