%@ page import="java.io.*" %> <%@ page import="javax.xml.transform.*" %> <%@ page import="javax.xml.transform.stream.*" %>
<%! /** Apply an XSL file to an XML file and return the results as a String. * @param strXMLfile String containing full URL to the XML file * @param strXSLfile String containing full URL to the XSL file * @return String containing the output of the transformation */ String ApplyXSL( String strXMLfile, String strXSLfile ) { // StringWriter is a child of java.io.Writer and can therefore be // used as an argument to a StreamResult constructor, which is // required by Transformer.transform(). StringWriter swResult = new StringWriter() ; try { // Use the static TransformerFactory.newInstance() method to instantiate // a TransformerFactory. The javax.xml.transform.TransformerFactory // system property setting determines the actual class to instantiate -- // org.apache.xalan.transformer.TransformerImpl. TransformerFactory tFactory = TransformerFactory.newInstance(); // Use the TransformerFactory to instantiate a Transformer that will work // with the stylesheet you specify. This method call also processes the // stylesheet into a compiled Templates object. Transformer transformer = tFactory.newTransformer( new StreamSource( strXSLfile ) ) ; // Use the Transformer to apply the associated Templates object to an XML // document (foo.xml) and write the output to a file (foo.out). transformer.transform( new StreamSource( strXMLfile ), new StreamResult( swResult ) ) ; // Return the result. return swResult.toString() ; } catch ( TransformerConfigurationException tfce ) { return tfce.toString() ; } catch ( TransformerException tfe ) { return tfe.toString() ; } } %> <% // This code assumes that the XML and XSL files reside in the same directory // as this JSP. Those file names are hard-code here, but they could be passed // as parameters from an HTML form or via some other such technique. String strXMLfilename = "tutorialRoster.xml" ; // replace with your XML file name String strXSLfilename = "tutorialRoster.xsl" ; // replace with your XSL file name // We need to construct a full URL to the XML and XSL files, including the // "http://" protocol specification, server name, and server port number (if // it's not the default of 80). The following code accomplishes this. String strFullPath = "http://" + request.getServerName() ; if ( request.getServerPort() != 80 ) strFullPath += ":" + request.getServerPort() ; // We then add the full path to this JSP and finally strip off this JSP's file // name and extension that follow the last forward slash (/). strFullPath += request.getRequestURI() ; strFullPath = strFullPath.substring( 0, strFullPath.lastIndexOf( "/" ) + 1 ) ; // We append the XML and XSL file names to the full path to pass them to our // ApplyXSL method, which applies the XSL file to the XML file and returns the // result as a string. Printing that String shows the result in the browser. out.println( ApplyXSL( strFullPath + strXMLfilename, strFullPath + strXSLfilename ) ) ; %>