XSLT - A simple Xalan tutorial

Xalan is an XSLT processor for transforming XML documents into XML, HTML or text.


Here's how to convert XML to HTML with Xalan in 5 steps.


Step 1. Download Xalan


Step 2. Copy Xalan and Xerces to Program Files, or wherever you store programs.

The Zip file you just downloaded contains a thousand files, but you need only 4.

 set dest=c:\apps\xalan-c
 mkdir %dest%
 cd /d c:\Users\Richard\Downloads
 
 copy /Y .\xalan_comb-1.11-x86-windows-VC100\XALANCPKG-11-31-VC100\bin\Xalan.exe %dest%
 copy /Y .\xalan_comb-1.11-x86-windows-VC100\XALANCPKG-11-31-VC100\bin\Xalan-C_1_11.dll %dest%
 copy /Y .\xalan_comb-1.11-x86-windows-VC100\XALANCPKG-11-31-VC100\bin\XalanMessages_1_11.dll %dest%
 copy /Y .\xalan_comb-1.11-x86-windows-VC100\XERCESCPKG-31-VC100\bin\xerces-c_3_1.dll %dest%

Step 3. Create your XML data file. E.g. books.xml


  <?xml version="1.0"?>
  <book-collection>
    <book>
      <author>Isaac Asimov</author>
      <title>Foundation</title>
    </book>
    <book>
      <author>Eckhart Tolle</author>
      <title>The Power of Now</title>
    </book>
  </book-collection>
  


Ads by Google

Step 4. Create your XSLT transform. E.g. books.xslt


  <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="html"/>
  
    <xsl:template match="/book-collection">
      <html>
        <body>
          <table border="1">
            <thead>
                <tr>
                <th>Author</th>
                <th>Title</th>
              </tr>
            </thead>
            <tbody>
              <xsl:for-each select="book">
                <tr>
                  <td>
                    <xsl:value-of select="author"/>
                  </td>
                  <td>
                    <xsl:value-of select="title"/>
                  </td>
                </tr>
              </xsl:for-each>
            </tbody>
          </table>
        </body>
      </html>
    </xsl:template>
  
  </xsl:stylesheet>
  


Step 5. Run Xalan


  c:\apps\xalan-c\xalan.exe books.xml books.xslt > books.html
  


All done. Open books.html for the output


  <html>
    <body>
    <table border="1">
      <thead>
        <tr>
          <th>Author</th>
          <th>Title</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Isaac Asimov</td>
          <td>Foundation</td>
        </tr>
        <tr>
          <td>Eckhart Tolle</td>
          <td>The Power of Now</td>
        </tr>
      </tbody>
      </table>
    </body>
  </html>
  

Ads by Google

Ask a question, send a comment, or report a problem - click here to contact me.

© Richard McGrath