spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / xml / column16

Transforming RSS into HTML and WAP II

Lotus Notes Developer
AMS Staffing Solutions
US-OH-Columbus

Justtechjobs.com Post A Job | Post A Resume
Developer News
Get Ready for Microsoft's 'Oslo' Modeling Tool
Latest Linux Hits Networking Flaws
Metasploit 3.2 Offers More 'Evil Deeds'

Continuing from where we left off with the last column, we turn to two major aspects of XSL style sheets:

Parameterization

The XSL specification of the World Wide Web Committee (W3C) allows for easy addition of runtime parameters to the formatting process:

"A top-level xsl:param element declares a parameter to the stylesheet; XSLT does not define the mechanism by which parameters are passed to the stylesheet."

Different implementations have different ways of specifying stylesheet parameters. The IBM alphaworks XSL processor LotusXSL has a start script with a special param command line parameter to pass them in. A programmatic way also exists using the method ApplyXSL.setStylesheetParams(XSLProcessor) directly from Java.

Let's modify last column's XSL style sheet for rendering RSS into plain HTML so that it recognizes the same parameters as my RSSViewerApplet:

NameTypeDescription
box.foregroundintbox foreground color
box.backgroundintbox background color
box.borderintbox border width
title.foregroundinttitle foreground color
title.backgroundinttitle background color
title.foreground.mouseoverinttitle foreground color on mouse-over
title.background.mouseoverinttitle background color on mouse-over
title.font.familyStringtitle font family
title.font.sizeinttitle font size
title.font.styleStringtitle font style
item.foregroundintitem foreground color
item.backgroundintitem background color
item.foreground.mouseoverintitem foreground color on mouse-over
item.background.mouseoverintitem background color on mouse-over
item.font.familyStringitem font family
item.font.sizeintitem font size
item.font.styleStringitem font style

First we need to add xsl:param parameters for the variables above:

<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform/1.0">

  <xsl:output method="html"/>

  <xsl:param name="box.foreground">black</xsl:param> 
  <xsl:param name="box.background">white</xsl:param> 
  <xsl:param name="box.border">3</xsl:param> 
  <xsl:param name="title.foreground">black</xsl:param> 
  <xsl:param name="title.background">white</xsl:param> 
  <xsl:param name="title.foreground.mouseover">white</xsl:param> 
  <xsl:param name="title.background.mouseover">black</xsl:param> 
  <xsl:param name="title.font.family">Helvetica, Arial, sansserif</xsl:param> 
  <xsl:param name="title.font.size">12</xsl:param> 
  <xsl:param name="title.font.style">bold</xsl:param> 
  <xsl:param name="item.foreground">black</xsl:param> 
  <xsl:param name="item.background">white</xsl:param> 
  <xsl:param name="item.foreground.mouseover">white</xsl:param> 
  <xsl:param name="item.background.mouseover">black</xsl:param> 
  <xsl:param name="item.font.family">Times, serif</xsl:param> 
  <xsl:param name="item.font.size">10</xsl:param> 
  <xsl:param name="item.font.style">italic</xsl:param> 

The parameter name can be any literal. I borrowed the dotted notation in this case from the conventions for Java properties. The default value for the parameter is specified as a text element within the param tags.

Then we define an inline CSS stylesheet in order to attach the formatting information of the XSL parameters to the right HTML elements. We could also have chosen to add the formatting directly to the elements, but separating style from content is a good idea even in generated data:

  <xsl:template match="/">
    <HTML><HEAD><STYLE TYPE="text/css">
	.box { color: <xsl:value-of select="$box.foreground"/>;
	       background-color: <xsl:value-of select="$box.background"/>;
	}
	A.title:link { color: <xsl:value-of select="$title.foreground"/>;
	       background-color: <xsl:value-of select="$title.background"/>;
		   font-family: <xsl:value-of select="$title.font.family"/>;
		   font-size: <xsl:value-of select="$title.font.size"/>;
		   font-weight: <xsl:value-of select="$title.font.style"/>;
	}
	A.item:link { color: <xsl:value-of select="$item.foreground"/>;
	       background-color: <xsl:value-of select="$item.background"/>;
		   font-family: <xsl:value-of select="$item.font.family"/>;
		   font-size: <xsl:value-of select="$item.font.size"/>;
		   font-weight: <xsl:value-of select="$item.font.style"/>;
	}
	A.title:hover { color: <xsl:value-of select="$title.foreground.mouseover"/>;
	}
	A.item:hover { color: <xsl:value-of select="$item.foreground.mouseover"/>;
	}
	</STYLE></HEAD><BODY><TABLE CLASS="box">
      <xsl:attribute name="BORDER">
        <xsl:value-of select="$box.border"/>
      </xsl:attribute>
      <xsl:apply-templates/>
    </TABLE></BODY></HTML>
  </xsl:template>

The xsl:value-of directive extracts the value of the selected XSL stylesheet parameter and inserts it into the generated output. The referenced parameter must be prefixed by a dollar sign.

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

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

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

</xsl:stylesheet>

The rest of the stylesheet remains largely unchanged, only some class attributes were added in the right places to reference the corresponding style information in the CSS inline style. This way we have elegantly added similar customization features to the server-side RSS display tool as exists in the RSSViewerApplet.

Here are some examples with different parameters:

And finally the XSL stylesheet for converting RSS into HTML for your own experiments and adaptations. After collecting some feedback I intend to add it to my collection of tools.

Next we adapt the original style sheet for WML output.

http://www.internet.com



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: BitLocker Encryption on Windows Server 2008
Go Parallel Article: Intel Thread Checker, Meet 20 Million LOC
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Tripwire Whitepaper: Seven Practical Steps to Mitigate Virtualization Security Risks
Internet.com eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
Anatomy of an Ajax Application · Popular JavaScript Framework Libraries: An Overview · Controllers: Programming Application Logic - Part 2
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
MS Access and MySQL · Cisco AutoQoS: VoIP QoS for Mere Mortals · While VoIP Adoption Explodes in Enterprise, Carrier Spending Lags

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

URL: http://www.webreference.com/xml/column16/index.html
Created: Aug 01, 2000
Revised: Aug 01, 2000