CSci 4608 Principles of Web Programming: XSL processing

This example shows how to process an XML file using XSL. In the example the output goes to standard output (System.out), but you can also direct it to the output of a servlet (the PrintWriter object of HttpResponse).


import java.io.*;
import javax.xml.transform.stream.StreamSource; 
import javax.xml.transform.stream.StreamResult; 

public class XSLProcessing {
    // this example illustrates how XSL transformations
    // can be done directly in the Java program
    // Both .xml and .xsl files are in the same directory 
    // as the program
    // The output is written to the standard output, 
    // but by setting the Result object one can direct
    // the output to the PrintWriter of an HttpResponse 
    // in a servlet

    public static void main(String [] args) 
    throws javax.xml.transform.TransformerException {
	// REPLACE THESE WITH YOUR FILE NAMES:
	String XMLSource = "food.xml";
	String XSLStylesheet = "food.xsl";
	
	// Instantiate a TransformerFactory.
	javax.xml.transform.TransformerFactory tFactory = 
                  javax.xml.transform.TransformerFactory.newInstance();

	// Use the TransformerFactory to process the stylesheet Source and
	// generate a Transformer.
	javax.xml.transform.Transformer transformer = tFactory.newTransformer
                (new javax.xml.transform.stream.StreamSource(XSLStylesheet));

	// Use the Transformer to transform an XML Source and send the
	// output to a Result object.
	transformer.transform
	    (new javax.xml.transform.stream.StreamSource(XMLSource), 
	     new javax.xml.transform.stream.StreamResult(System.out));
	
    }
   
}

The XML file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="example1.xsl" ?>
<breakfast_menu xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="food.xsd">
<food category="new">
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories>
</food>
<food category="new">
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
<calories>900</calories>
</food>
<food category="traditional">
<name>French Toast</name>
<price>$4.50</price>
<description>thick slices made from our homemade sourdough bread</description>
<calories>600</calories>
</food>
<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
<calories>950</calories>
</food>
</breakfast_menu>

The xsl file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited with XML Spy v4.2 -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="color">lightgreen</xsl:variable>
<xsl:template match="/breakfast_menu">
<html>
  <body>   
    <table>
    <xsl:for-each select="food[@category='new']">
    <tr>
    <td BGCOLOR="{$color}"><xsl:value-of select="*[1]"/></td>
    <td><xsl:value-of select="*[2]"/></td>
    <td><xsl:value-of select="*[3]"/></td>
    <td><xsl:value-of select="*[4]"/></td>
    </tr>
    </xsl:for-each>
    </table>
  </body>
</html>
</xsl:template>
</xsl:stylesheet>

CSci 2101 home page

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.