Grab Headlines From A Remote RSS File

by Nicholas Chase
Chase & Chase, Inc.
Wednesday, 3rd August 2005

The Primary Stylesheet

The ultimate goal is to generate HTML text that shows the information in an organized way, such as a list of links, included in the body of another page of information. The actual HTML output would be something like:

Listing 2. The output HTML

<h2>Adam Curry: Adam Curry's Weblog</h2>
<h3>News and Views from Adam Curry</h3>
<ul>
<li>
  <a href="http://www.blognewsnetwork.com/members/0000001/2003/07/24.html#a4158">weblog at work again</a>
 <p><a href="http://radio.weblogs.com/0001014/images/2003/07/24/adamwheely.jpg"><img src="http://radio.weblogs.com/0001014/images/2003/07/24/adamwheely.jpg" width="250" height="187.5" border="0" align="right" hspace="15" vspace="5" alt="A picture named adamwheely.jpg"></a>A few days ago I asked if anyone had taken pictures of me at the annual ...</li>
<li>  <a href="http://www.blognewsnetwork.com/members/0000001/2003/07/23.html#a4156">teens trouble with web</a>
  <p>According to a report from Northumbria University, most teenagers lack the <a href="http://www.web-user.co.uk/news/news.php?id=33621">information gathering skills</a> needed for using the internet efficiently. This sounds like it shouldn't be happening in ...</li>
...
</ul>

To create this HTML out of the XML, you'll need an XSLT stylesheet:

Listing 3. The simple stylesheet

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
  <xsl:apply-templates select="//channel"/>
  <ul>
    <xsl:apply-templates select="//item"/>
  </ul>
</xsl:template>
<xsl:template match="channel">
<xsl:apply-templates select="../image"/>
 <h2><xsl:value-of select="title"/></h2>
 <h3><xsl:value-of select="description"/></h3>
</xsl:template>
<xsl:template match="item">
  <li>
    <xsl:element name="a">
      <xsl:attribute name="href"><xsl:value-of select="link"/></xsl:attribute>
      <xsl:value-of select="title" />
    </xsl:element>
    <p><xsl:value-of disable-output-escaping="yes" select="description" /></p>
  </li>
</xsl:template>
<xsl:template match="image">
  <xsl:element name="img">
    <xsl:attribute name="src"><xsl:value-of select="url"/></xsl:attribute>
    <xsl:attribute name="style">float:left; padding: 10px;</xsl:attribute>
  </xsl:element>
</xsl:template>
<xsl:template match="language">
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
  <xsl:apply-templates select="//channel"/>
  <ul>
    <xsl:apply-templates select="//item"/>
  </ul>
</xsl:template>
<xsl:template match="channel">
<xsl:apply-templates select="../image"/>
 <h2><xsl:value-of select="title"/></h2>
 <h3><xsl:value-of select="description"/></h3>
</xsl:template>
<xsl:template match="item">
  <li>
    <xsl:element name="a">
      <xsl:attribute name="href"><xsl:value-of select="link"/></xsl:attribute>
      <xsl:value-of select="title" />
    </xsl:element>
    <p><xsl:value-of disable-output-escaping="yes" select="description" /></p>
  </li>
</xsl:template>
<xsl:template match="image">
  <xsl:element name="img">
    <xsl:attribute name="src"><xsl:value-of select="url"/></xsl:attribute>
    <xsl:attribute name="style">float:left; padding: 10px;</xsl:attribute>
  </xsl:element>
</xsl:template>
<xsl:template match="language">
</xsl:template>
</xsl:stylesheet>

The actual form of the page is entirely up to you, as is the data that you choose to include. In this case, you're simply creating a bulleted list of entries, with a title (if there is one) that links back to the original post and the description for each post.

To actually perform the transformation, you need to create a JSP page.


Options:
Printer Friendly
Email Friend

About The Author:

Nicholas Chase, a Studio B author, has been involved in Web site development for companies such as Lucent Technologies, Sun Microsystems, Oracle, and the Tampa Bay Buccaneers. Nick has been a high school physics teacher, a low- level radioactive waste facility manager, an online science fiction magazine editor, a multimedia engineer, and an Oracle instructor. More recently, he was the Chief Technology Officer of Site Dynamics Interactive Communications in Clearwater, Florida, USA, and is the author of four books on Web development, including XML Primer Plus (Sams). He loves to hear from readers and can be reached at nicholas@nicholaschase.com.

Developer Categories



Developer Tutorials
ASP
CGI & Perl
CSS
Flash
HTML
Java
JavaScript
MySQL
PHP
Python
XML

Developer Documentation

Developer Tools



Search our Developer Tutorials
  The DevSyndicate Network