Pregunta

Lo que estoy tratando de hacer es lo siguiente.

Pasa dos parámetros a una URL

  • tipo
  • doc_id

Una vez que se pasan al JSP a través de la URL, quiero aplicar una plantilla de tipo al doc_id xml.

Entonces, si el tipo es 001, el 001.xsl se aplica a doc_id.xml. La salida de esto no quiero que se almacene en un archivo, sino que se envía directamente al navegador.

¿Cómo haría esto usando XALAN y una página JSP?

¿Fue útil?

Solución

Sugeriría que este tipo de código vaya en un servlet en lugar de una página JSP. Si tiene alguna restricción específica que requiere un JSP, entonces el código podría modificarse para que funcione en una página JSP.

El sitio XALAN tiene un buen ejemplo utilizando un servlet que copiaré aquí por conveniencia: El original puede encontrarse aquí . En este ejemplo, codificaron los nombres de los archivos xsl y xml, pero eso es fácil de modificar para usar los nombres de los archivos generados como se describe. Lo importante es que la salida generada se transmite al navegador.

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();
   }  
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top