aspose file tools*
The moose likes XML and Related Technologies and the fly likes XML and XSLT to generate XHTML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XML and XSLT to generate XHTML" Watch "XML and XSLT to generate XHTML" New topic
Author

XML and XSLT to generate XHTML

Shelly Sahay
Greenhorn

Joined: Jul 07, 2009
Posts: 3
Can anyone please provide me a sample code snippet in java to convert xslt and xml to xhtml. My requirement is to replace jsp in my application with xhtml. I will take user input, convert to xml format. Use xslt and xml to generate xhtml at server side.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
Wouldn't it be simpler to just have your JSP generate valid XHTML?

Covering all the possible incorrect HTML which still works in most browsers is a realllllly tough job.

Bill
Shelly Sahay
Greenhorn

Joined: Jul 07, 2009
Posts: 3
Hi. Thanks for the reply. I understand that it is a tough job but it meets my busines requirements. I want to create a dynamic page for my users. In short, user is allowed to choose the layout desin and click save. After this I store the layout details in the form of xml. The next time this page is rendered to the user in the same session, I want the page to be in xhtml format. I have heard it is as good as jsp. Since the pages are created on the fly, I don't wish to generate seperate jsps for each user.

Could you help me with a sample code snippet that meets my requirement? It will be a great help. Thanks in advance.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

You really haven't ever seen an example of how to execute an XSL transformation in Java? That's all you're asking for, really. Try chapter 17 of ERH's online book.
Shelly Sahay
Greenhorn

Joined: Jul 07, 2009
Posts: 3
Hi I tried to generate XHTML on server side using xml source and XSLT source as input and performed XSL transformation of XML document into servlet output stream. But I get a blank page. I am clueless as to why this is happening. Following is a piece of code I have written in my servlet.


TransformerFactory tFactory = TransformerFactory.newInstance();
Source xslSource = new StreamSource(new File(path, xsl+".xsl")); t = tFactory.newTransformer(xslSource);
t.transform(xmlSource, new StreamResult(out));


Please help. Thanks in advance.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
I suggest proceeding with one of the following

1. perform the transformation offline to capture the transformation result to a file so you can inspect it
2. use TCPMON to capture the actual text output from the server
3. use the Firefox browser with one of the plugins that lets you examine the response content


One possible cause for a blank screen is a failure to close the response output stream - in which case the browser is still waiting for the rest of the page.

IF you are using the MS IE browser - turn off the "friendly HTTP error messages" checkbox in the Internet options - Advanced settings dialog so you can see actual error codes.

Bill
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

Why not just test your transformation code at the command line? Using a servlet as a test base means that you don't know whether the problem is in the servlet or in the transformation or in something else to do with the application server or the Internet.
 
wood burning stoves
 
subject: XML and XSLT to generate XHTML