IBM WebSphere Developer Technical Journal: XSL Transform Basics with WebSphere Application Server, Version 4.0x

This article shows how XSL transformations can be used with WebSphere Application Serve, and provides an example of how you can read XML into a servlet running in WebSphere Application Server and use XSLT to display the data in HTML form.

Joel Sundman (, IBM WebSphere Product Integration, IBM Rochester Lab

Joel Sundman is a staff software engineer at the IBM Rochester Lab in Rochester, Minnesota. Joel started his career in IBM Global Services helping customers design, build, and test both legacy and client-server applications. He currently works on the WebSphere Product Integration Test team. You can reach Joel at

18 April 2002


This article shows how XSL transformations can be used with WebSphere® Application Server. Basically, XML is a language that is used to define your own language. You do this by creating a Document Type Definition (DTD). A DTD defines all of the elements that you use in your "language." So any person or computer software that can read your DTD can "talk your language."

You have XML and you understand that it can be used to pass data between different programs and across the Web. How can you display that data on the Web?

  • Do you use an XML-enabled browser?
  • Do you convert it to HTML on the client side?
  • Do you convert it to HTML on the server side?

Yes, you can do any of these. Given the state of browser support for XML, it is often easier to convert XML to HTML on the server. This article provides an example of how you can read XML into a servlet running in WebSphere Application Server and use XSLT to display the data in HTML form. Xerces is an open source XML processor developed by Apache. Xalan is an XSLT processor that uses Xerces to transform XML. WebSphere Application Server, Version 4.0x ships with Xerces and Xalan, and they can be used to create, transform, or display XML. If you don't have Xerces or Xalan, you can download them from the Apache Web site. You can also use Cascading Style Sheets to display XML in a browser instead of transforming it on the server side. You may already be familiar with using Cascading Style Sheets with HTML.

The download file contains the sample application described in this article. Note that although this application was tested on WebSphere Application Server, Advanced Edition, Versions 4.01 and 4.02 running on Windows® 2000, it will also run on the Advanced Single Server Edition.

XML versus HTML

HTML and XML are not equivalent. HTML is meant for display and was defined using Standard General Markup Language (SGML). XML is intended to be a subset of SGML and is used to define your own markup language. XML is concerned with the data itself. SGML is a much more complex markup language than XML. There is a version of HTML that was created using XML and it is called XHTML. XHTML uses the tags of HTML, but is required to be well-formed because it is XML. Well-formed means that all tags have closing tags. In HTML, you are not required to use closing tags for some elements.

XML conversion technologies

Basically, there is XML 1.0 and that is it. Developers have created new technologies that can work with XML 1.0 instead of incorporating them into XML. This keeps XML 1.0 very simple. For transformation of XML, there is eXtensible Stylesheet Language Transformations (XSLT) and XSL formatting objects. This article describes how to use XSLT to transform XML with WebSphere Application Server, Version 4.0x.

XSLT is a stylesheet language that is used to transform XML into other XML or HTML. It is very easy to use, and I have included an XSL stylesheet in the example. Xalan is the XSLT parser that is used in the example to convert the incoming XML into HTML for display on the Web. XSL stylesheets are XML files that use XML syntax and thus must be well formed. For your templates in XSL, you cannot use the HTML tag <h1> without a closing </h1> because then your XML, the XSL file, will not be well-formed. XSLT has default templates that display the value of the elements in the XML as XML output, so an XSL stylesheet that does not specify any templates for converting the input XML will simply output an XML file with only the values of the elements and not the element tags or element attributes. For more information on XSLT, read the XSLT 1.0 specification.

XPATH is used with XSL to pick and choose which elements get templates applied to them. Without XPATH, there would be no way to denote in XSL which part of the XML input document to transform. Part of the official definition given in the XPATH 1.0 specification is "... a language for addressing parts of an XML document..."

Our sample application

In the download below are four files that can be used with WebSphere Application Server, Version 4.0x. The file GreatLakes.war is a module that can be deployed in WebSphere Application Server. It consists of a servlet, both the source code and compiled class file. The file lakes.dtd is the DTD file that lakes.xml file used in this example. The file greatLakes.xsl and greatLakes2.xsl are XSL stylesheets used by the servlet to convert the incoming XML into HTML.

There are several different ways to use XML with WebSphere Application Server:

  • XML can be input to the servlet or an EJB. It can be read in from a file or many other ways.
  • XML can be output of a servlet or EJB. It can be sent via JMS and MQSeries® or it can be stored in a file.

This example shows how to convert XML input into HTML output in a servlet. This simple example uses the following XML technologies. XSLT is used to transform the XML using Xalan XSLT processor, which uses Xerces to parse the XML and XSL files. XSLT uses XPATH to denote which templates apply to which elements. XSLT also uses XML namespace to denote that the elements of the XSL stylesheets are XML elements that belong to the XSLT namespace. This is abbreviated to XSL and put in front of each of the XSLT elements with a colon. For example, xsl:template is an element that belongs to the XSL name space. If you have an element named template and you wanted to use XSL stylesheets, that would be fine because the XSLT template element is already in the XSLT namespace. You can easily choose to use your own namespace or not, and XML parsers will not confuse the two.

We will use the files described above. The greatLakes.war file is ready to be run on WebSphere Application Server 4.0x. It contains the servlet source code and compiled code. You can extract the JavaTM source code to see what the servlet is doing.

The DTD supplied with this article, lakes.dtd, defines elements and element attributes for describing lakes.

Listing 1. lakes.dtd
<!ELEMENT greatlakes (lake+)> 
<!ELEMENT lake (size, bordering+)> 
<!ELEMENT size (#PCDATA)> 
<!ELEMENT bordering (#PCDATA)>

This DTD defines the following elements. The element gl:greatlakes is an element that has three children elements; gl:lake, gl:size, gl:border. Note that border is required to have one or more instances. The element gl:lake is made up of #PCDATA. #PCDATA is parsed character data. This is how you define raw text. It also has an attribute, name, which is required. The next two elements gl:size and gl:border also consist of #PCDATA.

Here is an XML file that depends on the lakes.dtd for validation. The file is a list of the lakes in the Great Lakes.

Listing 2. lakes.xml
<?xml version="1.0"?> 
<!DOCTYPE greatlakes SYSTEM "lakes.dtd"> 
   <lake name="Michigan"> 
   <lake name="Superior"> 
   <lake name="Huron"> 
   <lake name="Erie"> 
      <bordering>New York</bordering> 
   <lake name="Ontario"> 
      <bordering>New York</bordering> 

Let's say you have this XML document and you want to display it on the Web. Below is an XSL stylesheet that can be used to convert the lakes.xml file above into HTML.

Listing 3. greatlakes.xsl
<?xml version="1.0"?> 
<xsl:transform xmlns:xsl="" 
   <xsl:template match="/"> 
      <TITLE>The Lakes of the Great Lakes</TITLE> 
      <CENTER><H1>The Lakes of the Great Lakes</H1></CENTER> 
<!-- Template to handle lake elements--> 
<xsl:template match="lake"> 
   <TR><TD><H3><xsl:text>Lake </xsl:text> <xsl:value-of select= 
      "@name"/> </H3></TD><TD></TD></TR> 
   <TR><th><xsl:text>Size in square miles: </xsl:text></th><TD>  
      <xsl:value-of select="size"/></TD></TR> 
   <TR><th><xsl:text>States or Provinces bordering: </xsl:text></th><TD>  
      <xsl:apply-templates select="bordering"/></TD></TR> 
<!-- Template to handle bordering elements--> 
<xsl:template match="bordering"> 
   <xsl:for-each select="//bordering" /><xsl:value-of select="."/> 
   <xsl:text> </xsl:text> 

The attribute and value "xmlns:xsl="" in the xsl:transform element say to the XML processor that this element and its child elements belonging to the XSLT namespace, denoted "," will be prefixed with "xsl:" before the element name.

Below is the servlet code that reads in the XML file passed to it and applies the XSLT templates from greatlakes.xsl, and returns the new HTML document to the browser. This servlet imports the javax.xml.transform.* package. We pass to it the name of the XML file and XSL file that we want to use. When the servlet is called, it will create an XSLT transformer object using the XSL file that we pass in to the servlet. It will then read in and transform the XML file and return the result to the browser.

Listing 4. Code snippet from
// creating a transformer factory, 
// from which we can create a transformer. 
   TransformerFactory transFactory = TransformerFactory.newInstance(); 
   // creating a net transformer, xsltTransformer, from the factory  
   // and using xsl stylesheet that we passed into the servlet. 
   Transformer xsltTransformer = transFactory.newTransformer  
   // using the xsltTransformer to take the input XML file  
   // and transform it and return it to the browser. 

This servlet uses Xalan, an XSLT processor from Apache. This XSLT processor uses Xerces XML processor, also from Apache. You can get more information about it or you can download Xalan or Xerces from the Apache XML Project Web site. There is much information here about Xalan and you can learn quite a bit from the samples that are on this Web site.

Installing the GreatLakes.war file

To install the GreatLakes example application in WebSphere Application Server, Advanced Edition, Version 4.0x:

  1. Ensure that WebSphere Application Server is installed, and that you can access the snoop servlet running in the Default Server. For instructions on installing and configuring WebSphere Application Server 4.0, see the Installation section in the WebSphere Application Server Library. Copy the .war file to the installableApps directory under your "WebSphere AppServer" root (for example, C:\WebSphere\AppServer\installableApps).
  2. You can choose to either create a new server to install this application to, or utilize the Default Server. If you choose the former approach, you'll need to define a "working directory".
    Figure 1.

    If you choose to use the Default Server, the working directory is predefined to be <wasroot>\bin (for example, C:\WebSphere\AppServer\bin on WinNT/2K).

  3. Copy the files lakes.dtd, lakes.xml, greatLakes.xsl and greatLakes2.xsl to the working directory for your application server that you intend to use. This step is required so that the servlet knows where the files associated with this application are located.

To install the WAR file in Application Server:

  1. Select Console => Wizards => Install Enterprise Application. From the Specifying the Application or Module window, click Install stand-alone module.
  2. Click Browse next to Path and browse to the installableApps directory and select the GreatLakes.war file. Click Open. Now fill in the Application name field with XML example and the Context root for the Web module with /XMLexample.
    Figure 2.

    You can click Next without entering anything on the following windows:

    • Mapping Users to Roles
    • Mapping EJB Roles to Users
    • Binding Enterprise Beans to JNDI Names
    • Mapping EJB References to Enterprise Beans
    • Mapping Resource References to Resources
    • Specifying the Default Datasources for EJB Modules
    • Specifying Datasources for Individual CMP Beans
  3. Choose default_host on the Selecting Virtual Hosts for Web Modules window. On the Selecting Application Servers window, you can choose the Default Server or choose the server you created. On the last window, click Finish.
  4. Regenerate the plugin either in the Admin console by right-clicking on the node and choosing Regen WebServer Plugin or running GenPluginCfg.bat.
  5. Start the server that contains the GreatLakes Web module.

Running the GreatLakes example

After the server is started that contains the Web application, you can run the servlet in a browser with the following URL: http://localhost/XMLexample/GreatLakesServlet.

You should see an HTML page in your browser that looks like Figure 3 below.

Figure 3.

You can also run Xalan from a command line. If you are at a command line and in the directory with the lakes.xml and lakes2.xsl file, you can type the following command to run the XSLT processor and convert the XML into differently formatted XML. You must have xalan.jar and xerces.jar in your CLASSPATH. These are in the WebSphere lib directory in WebSphere Application Server, Advanced Edition 4.0x. You can download them from Apache if you need them.

java org.apache.xalan.xslt.Process -IN  
   lakes.xml -XSL greatLakes2.xsl -OUT lakes2.xml

You will see the output of this command in the new HTML file that you specify with the -OUT parameter. In this case, it is another XML file. If you view it, you will see that the XSL stylesheet greatLakes2.xsl did not covert the lakes.xml file into HTML. It converted it into another XML file that is laid out differently than lakes.xml. This new XML file, lakes2.xml, has the name of the lake as a child element of the element greatLake instead of an attribute of the element lake as in lakes.xml. The multiple bordering elements in lakes.xml are converted into a single instance of the states_provinces element in lakes2.xml.

If you have errors running the example

The most common error is that the servlet cannot find the XML, DTD or XSL file. Look into the standard outfile of the server running the servlet. It should give you a pretty good error that can tell you what the problem is.

If your browser cannot locate the servlet, you might have to regenerate your plugin using GenPluginCfg.bat on Windows or on UNIX®. If you are not using a standalone HTTP server, you can simply add the port to the application server in the browser window. For instance, if my application server is running on port 9082, type the following URL in your browser: http://localhost:9082/XMLexample/GreatLakesServlet.


XSLT can be used to convert XML files to HTML, WML, XHTML, or many other formats. You might choose which format to convert XML to depending on the client. XSLT can be used to alphabetize elements (using the xsl:sort tag) in one XML file and put them to another XML file.

Trying out this simple sample should help to provide you with a feel for how powerful XML can be, as well as a sense of how XML can be incorporated into your applications. Hopefully, trying this simple example will encourage you to explore other ways to make XML work for you. WebSphere supports these XML technologies and you might find something to make your application easier to maintain.

Special thanks to Tom Alcott, Melissa Modjeski, and Matt Kelm for their help with reviewing this paper and the sample code.


Code  ( HTTP | FTP )6 KB


  • Elliotte Rusty Harold & W. Scott Means, XML in a Nutshell, O'Reilly, 2001.
  • Brett McLaughlin, Java & XML, 2nd Edition O'Reilly, 2001.


developerWorks: Sign in

Required fields are indicated with an asterisk (*).

Need an IBM ID?
Forgot your IBM ID?

Forgot your password?
Change your password

By clicking Submit, you agree to the developerWorks terms of use.


The first time you sign into developerWorks, a profile is created for you. Information in your profile (your name, country/region, and company name) is displayed to the public and will accompany any content you post, unless you opt to hide your company name. You may update your IBM account at any time.

All information submitted is secure.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

Required fields are indicated with an asterisk (*).

(Must be between 3 – 31 characters.)

By clicking Submit, you agree to the developerWorks terms of use.


All information submitted is secure.

Dig deeper into WebSphere on developerWorks

ArticleTitle=IBM WebSphere Developer Technical Journal: XSL Transform Basics with WebSphere Application Server, Version 4.0x