Converting an XML to an HTML using XSLT with Xalan

Xalan is an Apache implementation of the W3C recommendations for XSL transformations, XSLT. XSLT allows you to transform xml documents in an HTML, another XML or whatever format you desire.

Download Xalan at

You can apply an XSL to an XML document with command line. Make sure xalan.xml is in your classpath before trying out this example. It will create an HTML file (check it out!) from an XML.

  java org.apache.xalan.xslt.Process -in customers.xml -xsl customers.xsl -out customers.html

customers.xml (!!remove the space between ? and xml):

<? xml version="1.0"?>
<!DOCTYPE customers SYSTEM "customers.dtd">
   <customer id="cust1">
      <name>Joris Van den Bogaert</name>
         <addressline1>Handelskaai 3</addressline1>
         <addressline1>A. Dewitstraat 50</addressline1>
   <customer id="cust2">
      <name>Alicia Kolesnikova</name>
         <addressline1>Handelskaai 3</addressline1>


<!ELEMENT customers (customer*)>
<!ELEMENT customer (name, address+)>
<!ELEMENT address (addressline1, addressline2, zip, location, country)>
<!ELEMENT addressline1 (#PCDATA)>
<!ELEMENT addressline2 (#PCDATA)>
<!ELEMENT location (#PCDATA)>
<!ELEMENT country (#PCDATA)>
<!ATTLIST customer

customers.xsl (!!remove the space between ? and xml):

<? xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="" version="1.0">
<xsl:template match="customers">
      <table border="1">
         <th>Customer ID</th>
<xsl:template match="customer">
      <td><xsl:value-of select="@id"/></td>
      <td><xsl:value-of select="name"/></td>
      <td><xsl:apply-templates select="address"/></td>
<xsl:template match="address">
   <xsl:value-of select="addressline1"/>
   <xsl:if test="string(addressline2) != string('')">
      &#160;<xsl:value-of select="addressline2"/>
   <xsl:value-of select="zip"/>&#160;<xsl:value-of select="location"/> 
   [<xsl:value-of select="country"/>]<br/>