tags:

views:

244

answers:

1

Hey,

What I am is trying to do is the following.

Pass two parameters to a URL

  • type
  • doc_id

Once they are passed to the JSP via the URL I want to apply a the type template to the doc_id xml.

So if the type is 001 then the 001.xsl is applied to doc_id.xml. The output of this I don't want stored in a file but rather directley outputed to the browser.

How would I go about doing this using XALAN and a JSP page?

A: 

I would suggest that this type of code goes in a servlet rather than a JSP page. If you have some specific constraint that requires a JSP then the code could be modified to work on a JSP page.

The XALAN site has a nice example using a servlet which I'll copy here for convenience: The original can be found here. In this example they hard coded the names of the xsl and xml files but that is easy to modify to use your file names generated as you described. The important thing is that the generated output is streamed to the browser.

public class SampleXSLTServlet extends javax.servlet.http.HttpServlet {

    public final static String FS = System.getProperty("file.separator"); 
    // Respond to HTTP GET requests from browsers.

    public void doGet (javax.servlet.http.HttpServletRequest request,
                 javax.servlet.http.HttpServletResponse response)
                 throws javax.servlet.ServletException, java.io.IOException
   {
     // Set content type for HTML.
     response.setContentType("text/html; charset=UTF-8");    
     // Output goes to the response PrintWriter.
     java.io.PrintWriter out = response.getWriter();
     try
     {  
        javax.xml.transform.TransformerFactory tFactory = 
           javax.xml.transform.TransformerFactory.newInstance();
        //get the real path for xml and xsl files.
        String ctx = getServletContext().getRealPath("") + FS;        
        // Get the XML input document and the stylesheet, both in the servlet
       // engine document directory.
       javax.xml.transform.Source xmlSource = 
            new javax.xml.transform.stream.StreamSource
                         (new java.net.URL("file", "", ctx+"foo.xml").openStream());
       javax.xml.transform.Source xslSource = 
            new javax.xml.transform.stream.StreamSource
                         (new java.net.URL("file", "", ctx+"foo.xsl").openStream());
       // Generate the transformer.
       javax.xml.transform.Transformer transformer = 
                         tFactory.newTransformer(xslSource);
       // Perform the transformation, sending the output to the response.
      transformer.transform(xmlSource, 
                       new javax.xml.transform.stream.StreamResult(out));
     }
     // If an Exception occurs, return the error to the client.
    catch (Exception e)
    {
       out.write(e.getMessage());
       e.printStackTrace(out);    
    }
    // Close the PrintWriter.
    out.close();
   }  
}
Vincent Ramdhanie