Transforming RSS into HTML and WAP I (2/2) - exploring XML | WebReference

Transforming RSS into HTML and WAP I (2/2) - exploring XML

Transforming RSS into HTML and WAP I

A Java Servlet

So what solution offers the best combination of portability and power? In many cases, the answer is Java servlets. Java servlets combine the advantages of a server-based solution with the qualities of Java. They are platform-independent, easy to integrate with existing Web servers, and there are many XML tools written in Java. Popular tools include:

On the following pages we used the LotusXSL processor to develop an XSL style sheet that turns RSS into HTML. We start with a shortened version of this column's RSS:

<rss> 
  <channel> 
    <title>eXploringXML</title> 
    <link>https://exploringxml.com</link> 
    <description>Free tutorials, examples, and tools for XML 
	  at WebReference</description> 
  </channel> 
  <image> 
    <title>eXploringXML</title> 
    <url>https://webref.com/xml/art/logo.gif</url> 
    <link>https://exploringxml.com</link> 
  </image> 
  <item>
    <title>Registering and publishing with RSS</title>
    <link>https://webref.com/xml/column14/</link>
  </item>
  <item>
    <title>Creating RSS files for your Web site</title>
    <link>https://webref.com/xml/column13/</link>
  </item>
</rss> 

The HTML output should look like this:

eXploringXML
Registering and publishing with RSS
Creating RSS files for your Web site

Next we develop the XSL style sheet step-by-step. First we need to set up the HTML page:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="https://www.w3.org/XSL/Transform/1.0">
  <xsl:output method="html"/>
  <xsl:template match="/">
    <HTML><BODY><TABLE BORDER="1">
      <xsl:apply-templates/>
    </TABLE></BODY></HTML>
  </xsl:template>

Then we generate a table header row for the <channel> element:

  <xsl:template match="channel">
    <TR><TH><A>
      <xsl:attribute name="HREF">
        <xsl:value-of select="link"/>
      </xsl:attribute>
      <xsl:value-of select="title"/>
      </A></TH></TR>
      <xsl:apply-templates/>
  </xsl:template>

Next a normal table row for each <item> element:

  <xsl:template match="item">
    <TR><TD><A>
      <xsl:attribute name="HREF">
        <xsl:value-of select="link"/>
      </xsl:attribute>
      <xsl:value-of select="title"/>
      </A></TD></TR>
      <xsl:apply-templates/>
  </xsl:template>

Finally we need to suppress the default text output and close the style sheet:

  <xsl:template match="text()"/>
</xsl:stylesheet>

That is all for now. Of course the output is neither pretty nor customizable but we will improve on this soon. Furthermore, we will show how to adapt this solution to output WAP WML.

Conclusion

There are many ways to process XML content such as RSS files. A servlet-based architecture avoids most of the shortcomings of other solutions, such as limited portability and non-standard libraries. In one of the next installments we will look at how to build a XML Java servlet toolkit that can be used with the most popular Web servers out there.

https://www.internet.com

Produced by Michael Claßen
All Rights Reserved. Legal Notices.

URL: https://www.webreference.com/xml/column15/2.html
Created: Jul 18, 2000
Revised: Jul 18, 2000