XSLT Servlet

This servlet transforms an XML document using an XSLT stylesheet. Probably there are other Servlet implementations of this kind, but I couldn’t find one with the features I need available under an Open Source license, so I wrote this one.

Features

  • the XML document and the stylesheet can be retrieved from the local webapp or from the network (using java.net).
  • Stylesheet is preloaded at startup and then periodically reloaded in the background in order to optimize performance.
  • JSP EL expressions can be used in the Servlet init params.
  • Parameters can be passed to the stylesheet.
  • XSLT output properties can be controlled by the Servlet.
  • Not tied to a specific XML parser or XSLT processor.
    This Servlet uses javax.xml.transform (aka TrAX) for XSLT processing.

This is a sample web.xml configuration:

<servlet>
  <servlet-name>myTransformation</servlet-name>
  <servlet-class>it.kataweb.xslt.servlet.XSLTServlet</servlet-class>
  <init-param>
    <param-name>xml</param-name>
    <param-value>http://domain.org/service?param=${param.myParam}</param-value>
  </init-param>
  <init-param>
    <param-name>xslt</param-name>
    <param-value>/WEB-INF/xslt/myStylesheet.xslt</param-value>
  </init-param>
  <init-param>
    <param-name>xsltReloadInterval</param-name>
    <param-value>60</param-value>
  </init-param>
</servlet>

For reference documentation see the Javadocs included in the distribution.

Legal

This Servlet is Open Source software distributed under the Apache License Version 2.0.

Requirements

Download

One Comment

  1. Dominic Chambers says:

    I found a bug with this where relative context paths don’t work on windows — not sure if this is for all window users, or perhaps for those whose context is on a non-C drive. Here is an updated XSLTServlet.stringToURL() method that fixes this:

    private URL stringToURL(String string) throws MalformedURLException {
    String url;
    if (string.startsWith(”/”)) {
    String contextPath = getServletContext().getRealPath(string);
    url = “file://” + ((contextPath.startsWith(”/”)) ? contextPath : “/” + contextPath);
    } else {
    url = string;
    }

    return new URL(url);
    }

Leave a Reply