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

  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);
    }

  2. Remy says:

    Downloads gone?!? Where?

  3. Remy says:

    Downloads gone?
    Use url:

    http://flavio.tordini.org/xslt-servlet

    And there they are!

  4. Johni Linnx says:

    Can you give an example of how to run this servlet with JBoss?
    I have successfully built the jar file
    ————
    [jar] Building jar: C:\xslt-servlet-1.1.0\build\jar\xslt-servlet-1.1.0-dev.jar
    ————
    To run it, how do I set up my jboss 5.1 server to run it with a sample xml file and a sample xslt file?
    Would Appreciate your help.
    Also, how to set the output to a pdf output?

  5. Flavio says:

    Hi Johni, wow it’s been ages since I wrote this servlet. Never actually used JBoss, but you should configure the servlet in your web.xml like in the example above. Don’t know about PDF output, is XSLT even able do it?