This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Hi all, It took me a while to find the right way of doing this, and I thought I would share it since it has saved me quite a lot of time and effort. To transform XML to HTML using XSL using a servlet is surprisingly simple. (You need xalan.jar in your path): (1) import org.apache.xalan.xslt.*; (2) In your service method: String sheet = "myStyleSheet.xsl"; String doc = "myXML.xml"; XSLTProcessor processor = XSLTProcessorFactory.getProcessor(); processor.process(new XSLTInputSource(new FileInputStream(doc)), new XSLTInputSource(new FileInputStream(sheet)), new XSLTResultTarget(res.getOutputStream())); I have left out the try/catch block to save space. This example processes a XML file and stylesheet which are both files, but you can just as easily process using InputStreams from a URL, or some other source. In my application, I call a URL of another servelt on the same server, which returns back a dynamic XML document each time, based un URL parameters. Have fun! Bill
Hello folks, I was wondering where cand I find a full example of using servlets for processing XML files to transform them in HTML with XSL. I want to implement a catalog with books in XML and I want a servlet to read that XML file and put on the web the HTML file with XSL. I downloaded xalan form apache but their example about servlets doesn't show the right things to do what I want.In fact it's a no use example. Can you help me? Thank you, Bogdan
Bogdan, Checkout the Cocoon framework by Apache. It is an integrated framework that has a servlet engine, a parser and an XSLT engine that seamlessly transform an XML into HTML formats. Good luck, ------------------ Ajith Kallambella M. Sun Certified Programmer for the Java�2 Platform. IBM Certified Developer - XML and Related Technologies, V1.
Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Sakthiusa , Welcome to javaranch. PROPER NAMES ARE NOW REQUIRED Please check out the official naming policy of javaranch. Please reregister yourself with proper first & last name & help maintain the decorum of the forum.
Originally posted by Bill Pearce: [B]As requested, here is my complete service method for using a URL to another servlet as the source of your XML document:
Hope this helps, Bill [This message has been edited by Bill Pearce (edited September 19, 2000).][/B]
Hi, i have no problem when i compiled the java program but encountered the below error when using my browser to view the servlet. Error: java.lang.ClassNotFoundException: org.apache.xalan.xslt.XSLTProcessorFactory PLs help. I have already set my classpath to xalan.jar and xerces.jar. I'm using Xalan on manta-Linus. Thanx.
Joined: Mar 17, 2000
Try moving the jar files to JREROOT\lib\ext directory.
Joined: Jun 21, 2001
Originally posted by Ajith Kallambella: Try moving the jar files to JREROOT\lib\ext directory.
Hi, thanx for the reply. I have solved the problem by specifying the CLASSPATH in the tomcat conf files, tomcat.sh or jspc.sh
Joined: Jun 21, 2001
looks like the whole bunch of code do not work in xalan2 anymore. Anybody has any luck working with xalan2 using TransformerFactory??