[jdom-interest] XML and XSL

Daniel Hoppe hoppe at sitewaerts.de
Mon Aug 20 02:10:58 PDT 2001

Hi Peter,

Nothing weird so far, sounds familiar ;-). You want to transform XML to HTML
and return the result to the browser. Do you need to transform with the same
stylesheet several times? 
I don't know of a function of the node to transform, but what you might do
is the following:

- Use JAXP / Trax to obtain a TransformerFactory (http://java.sun.com/xml/),
get a Templates instance and a Transformer instance for each Transformation
and use this to transform your input. 
- As you probably start with a JDOM DOM you might want to check
http://www-106.ibm.com/developerworks/java/library/x-tipjdom.html for an
example how to fill the gap between JDOM and the transformer efficiently.

This basic example could (better 'might', untested...) do what you need:

package de.sitewaerts.futuna.cs.common;

import java.util.*;
import java.io.*;
import javax.naming.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.*;

public class XMLServlet extends HttpServlet
  private Category log;
  private Templates tps;

  public ComponentStartupServlet()
    log = Category.getInstance(this.getClass().getName());

  public void init() throws ServletException
    ServletConfig sc = getServletConfig();
    String sStyleSheet = sc.getInitParameter("stylesheet");
    if (sStyleSheet == null)
	throw new ServletException("Please specify the init parameter
    	Source sourceXSL = new StreamSource(new FileInputStream(sStyleSheet
      TransformerFactory tf = TransformerFactory.newInstance();
      tps = tf.newTemplates(sourceXSL);
    catch (TransformerConfigurationException tce)
	log.error("Problem creating template", tce);
	throw new ServletException("Problem creating template:" +

  public void doGet (HttpServletRequest request, HttpServletResponse
    throws ServletException, IOException

	response.setContentType("text/html; charset=UTF-8");
	OutputStream osTarget = response.getOutputStream();
      Source sourceXML = new StreamSource(...); // Get an InputStream or
whatever gives you your XML document
      Result resultTarget = new StreamResult(osTarget);
      Transformer t = tps.newTransformer();

/*	Maybe the stylesheet needs some parameters:

      if (hmParameters != null)
        Iterator it = hmParameters.keySet().iterator();
        String sKey = null;
        while (it.hasNext())
          sKey = (String)it.next();
          t.setParameter(sKey, hmParameters.get(sKey));

      t.transform(sourceXML, resultTarget);
      log.info("Transformed with transformer  '" + t.getClass().getName() +
    catch (TransformerException te)
	log.error("A TransformerException occured", te);
      throw new ServletException("A TransformerException occured: '"
        + te.getMessage());

Basically that's what I would do, maybe someone one the list has a better
solution? You could of course wrap the same functionality in a Custom Tag on
a JSP or directly with a JSP. 



> -----Original Message-----
> From: jdom-interest-admin at jdom.org
> [mailto:jdom-interest-admin at jdom.org]On Behalf Of Peter Barraud
> Sent: Monday, August 20, 2001 9:09 AM
> To: Jdom
> Subject: [jdom-interest] XML and XSL
> Hi,
> Is there a way to use an XML file with an XSL file and render 
> the output to
> a browser. Ok I know that sound weird but here is what I want to do:
> I have an XML file and an XSL file that contains parsing code 
> to get HTML
> out of the XML. So what I want is that a servlet/JSP will 
> return the result
> as an output to the browser.
> I have done this in ASP and MSXML. There is a function (of 
> the XML DOM)
> called Transform node that takes an XSL as an argument and 
> return the parsed
> result.
> I hope this is not too very confusing, and my apologies if I 
> have used some
> vague terms but any help would be really really appreciated
> PB
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/yo
uraddr at yourhost.com

sitewaerts GmbH
Hebelstraße 15
D-76133 Karlsruhe

Tel: +49 (721) 920 918 0
Fax: +49 (721) 920 918 29

More information about the jdom-interest mailing list