Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_jsp thread: Display XML with XSLT in Java


Message #1 by "Raoul van Balen" <raoul.van.balen@a...> on Mon, 10 Jun 2002 15:50:16
> Does anyone have an example, a paper, a link, an article which 
> explains 
> how to transform an xml-document with an xsl-stylesheet in Java 
> and 
> displays it in your browser?

<!--Detects client browser type and displays custom start page-->

<!--
    Author: Linda Postniece
    Date: 27 May 2002
-->

<%@ taglib uri="http://jakarta.apache.org/taglibs/xsl-1.0"
prefix="xsltlib" %>

<% 
   String client = request.getHeader("User-Agent");
   String accept = request.getHeader("Accept");
   System.out.println("client: " + client);
   System.out.println("request arrived here at " + new java.util.Date
());
   // DESKTOP
   if ((client.indexOf("MSIE") >= 0) || (client.indexOf("Mozilla") >= 
0))
     {
       response.setContentType("text/html");
%>
       <xsltlib:apply xml="/xml/discussion.xml" 
xsl="/xsl/xhtml_showMessage.xsl" />
<%
     }
   // WAP
   else if (accept.indexOf("text/vnd.wap.wml") >=0)
     {
       response.setContentType("text/vnd.wap.wml");
%>
       <xsltlib:apply xml="/xml/discussion.xml" 
xsl="/xsl/wml_showMessage.xsl" />
<%
     }  

   // IMODE
   else if (accept.indexOf("text/html") >=0)
     {
       response.setContentType("text/html");
%>
       <xsltlib:apply xml="/xml/discussion.xml" 
xsl="/xsl/chtml_showMessage.xsl" />
<%
     }
   // XML
   else
     {
       response.setContentType("text/xml");
%>
       <%@ include file="/xml/discussion.xml" %>
<%
     }
%>

In this particular example, we're using XSLT to transform standard XML 
to different markup types depending on the client.

This is the main bit:

<xsltlib:apply xml="/xml/discussion.xml" 
xsl="/xsl/chtml_showMessage.xsl" />

It uses a tag library (see the top of page).

If you're interested in further details (or more sample code), pls 
email me directly.

Cheers,

Linda


  Return to Index