You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by Joe Nolting <jn...@xsconsulting.com> on 2006/04/03 16:10:02 UTC

Using xalan in a servlet w/ xforms


Hello,

I am trying to use Xalan in a servlet to transform xml submitted from an
xform (specifically the chiba project).  I pass in the path to the
stylesheet and extract the xml from the request body.  After the
transform I am returned plain text that appears the transformer
attempted to format it, instead of the html that I expected.  However,
if I pass in the path to an xml file as a param and use that file
instead of the xml from request in the transform the transformer does
return html.  I'm at a loss here.    

Any hints, help, or suggestions would be greatly appreaciated.

Thank you.


public void doPost(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {

	String xml      = new String("");
	String thisLine = new String("");
	out = res.getWriter();
	
	//get path to xsl file.
	String xslFile    = req.getParameter("xslt");	
			
	res.setContentType("text/html; charset=UTF-8");
			
	
	if ( xslFile == null) {
		out.println(
		"<h1>No input for xslFile</h1>");
		return;
	}
        
	String ctx = getServletContext().getRealPath("") + FS;
	xslFile = ctx + xslFile;

	//extract xml from request		
	BufferedReader br = req.getReader();
	
	while ((thisLine = br.readLine()) != null) { 
			          
		xml = xml.concat(thisLine);
			                       
	}	
		
	DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
	factory.setNamespaceAware(true);
				
	DocumentBuilder builder  = factory.newDocumentBuilder();
	

	Document xmlDocument = builder.parse(new InputSource(new
StringReader(xml)));

	//get a domsource			    	   
	DOMSource xmlDomSource = new DOMSource(xmlDocument);
			
			
         
	TransformerFactory tFactory = TransformerFactory.newInstance();
			
	Transformer transformer = tFactory.newTransformer(new
StreamSource(xslFile));
			

	// Perform the transformation.
	transformer.transform(xmlDomSource, new
StreamResult(out));


}