IBM WebSphere Studio Working with XML

Carol Jones
Senior Technical Staff Member
IBM
December 1999

Introduction

XML (Extensible Markup Language) is a great way to exchange data between two applications. In this paper, we'll show you some interesting ways to use XML in your WebSphere applications.

The remainder of this paper has the following sections:

  • XML Basics
    First, we'll run down the basics of XML, to make sure you understand the terminology and concepts.
  • XSL Basics
    Then we'll give a quick overview of XSL, which is a way of formatting XML documents.
  • Data Islands
    Finally, we get to the good stuff: we'll show you how to use XML and XSL inside your web pages.

XML Basics

You're already familiar with HTML, which is a tag language that describes the structure of a web page and its visual presentation. Although each browser might render the HTML a little bit differently, you can get a pretty good idea of how the page will look from its tags, such as heading and table tags.

Extensible Markup Language (XML) is slightly different. It is a markup language that describes data in a human readable format, but without any information about how the data is to be displayed. Here's a small example:

<?xml version="1.0"?>
<spec>
  <title>Configuration for Acme Mega Server</title>
  <model>AMS 9000</model>
  <parts>
    <part>
      <number>9000</number>
      <description>Model 900</description>
      <list-price>$2,000.00</list-price>
    </part>
    <part>
      <number>1501</number>
      <description>32X CD-ROM</description>
      <list-price>$50.00</list-price>
    </part>
    <part>
      <number>1808</number>
      <description>Acme 17 inch Display</description>
      <list-price>$300.00</list-price>
    </part>
    <part>
      <number>2900</number>
      <description>Acme Printer</description>
      <list-price>$150.00</list-price>
    </part>
  </parts>
  <list-subtotal>$2,500.00</list-subtotal>
</spec>

Marking up data with XML makes it easy to exchange data between two applications, because they don't have to understand anything about file formats. All they need to do is understand the XML tags, which makes the data really easy to parse. (You don't have to write a parser for it, you can just use the one provided with the WebSphere Application Server).

There really aren't any specific tags in XML, you can make up your own XML language with any tags you need to describe the data. To construct your own XML language, you create a specific Document Type Definition (DTD), In our product specs example, the DTD would include a rule that states that the <part> element consists of three other elements called <number>, <description>, and <price>. The rule would also indicate if any of the elements are optional, repeated, or have default values. You don't always need a DTD, because the XML parser can figure out the structure of the document by reading the elements.

XSL Basics

Although XML is easy to read and easy to exchange with other applications, it's not very suitable for displaying on a web page, precisely because it does not include any information about what the visual presentation should be.

XSL (eXtensible Stylesheet Language) is the language you use to tranform or format XML into some other kind of document, such as an HTML page. When you run the XML document through an XSL processor, the XML information will be evaluated and completely transformed into something else.

All the transformations happen through a series of tests that try to match patterns in the XML data. Here's a really trivial example:

<xsl:stylesheet>
  <xsl:template match = "/">
    <HTML>
    <BODY>
      <xsl:apply-templates select="spec"/>
    </BODY>
    </HTML>
  </xsl:template>

  <xsl:template match="spec">
  <H1>
    <xsl:value-of select="model"/>
  </H1>
  </xsl:template>
</xsl:stylesheet>


That simple stylesheet has the effect of producing an HTML page, that has the HTML tags, BODY tags, and a H1 tag displaying the model name of the product spec. The output would look like this:

  <HTML>
  <BODY>
    <H1>
      AMS 9000
    </H1>
  </BODY>
  </HTML>


Data Islands

Okay, now let's put everything together. You have some XML data, ready to go, and you have a style sheet, ready to go. WebSphere Application Server supplies the XML and XSL support you need to display this data right in a JSP page. To make it work, we'll start by writing a very simple Java bean. Let's walk through the code, step-by-step:

As always, you start off by importing the classes that you need. Here, we need to import the XML and XSL classes:

	  import java.lang.*;
	  import java.util.*;
	  import com.ibm.xml.parser.*;
	  import com.lotus.xsl.*;
	  import java.io.*;
	  

Now, we can declare the class, all the variables, and the getter and setter methods:

	  public class DataIslandBean extends java.lang.Object implements java.io.Serializable
	  {
	  	protected String xml = "";
	  	protected String xsl = "";
	  	protected String document = "";
	  
	  	public String getXml()
	  	{
	  		return xml;
	  	}
	  
	  	public void setXml(String value)
	  	{
	  		this.xml = value;
	  	}
	  
	  	public String getXsl()
	  	{
	  		return xsl;
	  	}
	  	public void setXsl(String value)
	  	{
	  		this.xsl = value;
	  	}
	  
	  	public String getDocument()
	  	{
	  	  return document;
	  	}
	  
	  	public void setDocument(String value)
	  	{
	  		this.document = value;
	  	}
	  }
      

All the real work is done in the getDocument method. It's very simple. It looks for the URL of the XML data and the URL of the XSL. Once those variables are set up, we simply call the XSL Processor to transform the XML, using the instructions in the XSL. The resulting data is written to the servlet's output stream. That's it!

	  public String getDocument()
	  {
	    try
	    {
	      ByteArrayOutputStream stream = new ByteArrayOutputStream();
	      PrintWriter pw = new PrintWriter(stream);
	      XSLProcessor processor = new XSLProcessor();
	      processor.process(xml, xsl, pw);
	      document = stream.toString();
	    }
	    catch(Exception e)
	    {
	      document = "Error in xsl processing " + e.getMessage();
	    }
	    return document;
	  }
	  

Using this bean in a Java Server Pages file is really easy. All you need is to add a bean tag into your page, and then call the getDocument method where you want the data to be displayed. Since you are passing in a URL to the bean, you don't really have to have the XML data in a file; you could write a bean or JSP page to compute it on the fly.

	  <HTML>
	  <HEAD>
	  <TITLE> XML Data Island Sample </TITLE>
	  </HEAD>
	  <BODY>
	  <BEAN type="DataIslandBean" name="dataIslandBean" introspect="no" create="yes" scope="request">
	  <PARAM name="xml" value="http://localhost/xml/specs.xml">
	  <PARAM name="xsl" value="http://localhost/xml/specs.xsl">
	  </BEAN>
	  <H2>Data Island Example</H2>
	  <P>
	  <%= dataIslandBean.getDocument() %>
	  </P>
	  </BODY>
	  </HTML>
	  

When you're ready to test, simply copy the bean class to the WebSphere Application Server's servlets folder, and then copy the JSP page to your HTTP server's document root folder. Then open the JSP page in a browser. (Note: if you're running Version 3.0 or higher of the WebSphere Application Server, don't forget to install the Bean Scripting Framework code first (which can be downloaded from http://www.alphaworks.ibm.com ).

For this example, we wrote a much more elaborate stylesheet, and then tested it in the browser. Here's the result:

XML Page

back