Helping ordinary people create extraordinary websites!
HOME TUTORIALS SCRIPTS WEB HOSTING BLOG FORUM
Get Our Newsletter
Email:

Grab Headlines From A Remote RSS File

By Nicholas Chase
2003-12-19


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.

Tutorial Pages:
» Retrieve Syndicated Content, Transform It, & Display The Result
» The Source File
» The Primary Stylesheet
» The Basic JSP Page
» Transforming The File
» Adjusting For Multiple Formats
» Choosing A Version
» Caching The Feed
» Conclusion
» Resources


First published by IBM developerWorks


Related Tutorials:
» Starting with XML
» Performing Client-Side XSL Transformations
» Create a Google Sitemap for your Web Site
» XML and Scripting Languages
» Parsing Comma-Separated Values
» XML Security Suite: Increasing the Security of E-Business