Using JAXP with the Xalan XSLT processor in JRun, you can transform XML documents and output them with servlets or JSPs. To transform XML, you need an XML input source and an XSL style sheet. You can transform the XML into HTML or any other markup language (such as WML or SGML).
XSL style sheets indicate to the client how you want the content of the XML elements to be formatted. You can use XSL style sheets to produce a set of HTML tags as an HTML page.
In the BODY tag of the HTML page, the following example XSL style sheet transforms text labeled HomePageLinks into a hyperlink, then outputs an HTML page:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html"/> <xsl:template match="/"> <html> �<HEAD> ��<TITLE>Home Page</TITLE> �</HEAD> �<body> ��<h2>Available Links:</h2><br/> ��<xsl:apply-templates select="HomePageLinks"/> �</body> </html> </xsl:template> <xsl:template match="A"> �<xsl:element name="A"> ��<xsl:attribute name="HREF"> ���<xsl:value-of select="@HREF"/> ��</xsl:attribute> ��<xsl:value-of select="."/> �</xsl:element> <br/> </xsl:template> </xsl:stylesheet>
For information on using XSL style sheets, see "Resources".
The Transformer object is available in JAXP. It processes XML input from a variety of sources and writes the resulting transformation to any output. To create a Transformer object, you use the newTransformer method of the TransformerFactory.
The following example servlet gets the XML source file and transforms it with the rules in the previous XSL file:
import javax.servlet.*;
import javax.servlet.http.*; import javax.xml.transform.*; import javax.xml.transform.stream.*; import java.io.*; import java.net.*;
public class XSLTHomePage extends HttpServlet {
�public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
��PrintWriter out = response.getWriter();
��try {
���//create TransformerFactory
���TransformerFactory tFactory = TransformerFactory.newInstance();
���// Get the XML input document and the style sheet
���ServletContext sc = this.getServletContext();
���URL xslURL = sc.getResource("/homepagelinks.xsl");
���URL xmlURL = sc.getResource("/homepagelinks.xml");
���Source xslSource = new StreamSource(new java.net.URL(xslURL.toString()).openStream());
���Source xmlSource = new StreamSource(new java.net.URL(xmlURL.toString()).openStream());
���// Generate the transformer.
���Transformer transformer = tFactory.newTransformer(xslSource);
���// Perform the transformation, sending the output to the response.
���transformer.transform(xmlSource, new StreamResult(out));
��} catch (Exception e) {
���out.println(e.getMessage());
��}
�}
}
For more detailed information on using JAXP, see the following:
To see a sample servlet, start the samples JRun server and open a browser to http://localhost:8200/techniques.
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/jrun/4/Programmers_Guide/servletxml6.htm