head8k
02-06-2003, 02:56 PM
Please could somebody write me an XSL stylesheet to display this RSS feed:
http://diveintomark.org/xml/fosnews.php
I have written code to fetch and store a local copy of the feed which I then want to parse (using XSL on the server-side) into HTML for a webpage.
Why is XSL so difficult to write? :confused: I would be very grateful for an example.
mpjbrennan
02-07-2003, 01:11 PM
Here's a very quick attempt - I'm sure it can be improved upon. For example, I haven't figured out yet how to deal with all the < and > url's in the description paragraphs.
patrick
-----------------------
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates select="*" />
</body>
</html>
</xsl:template>
<xsl:template match="channel">
<span style="font-weight:bold; font-family:serif font-size:16pt; color:green">
<xsl:apply-templates />
</span>
<br/>
</xsl:template>
<xsl:template match="channel/description">
<span style="font-weight:normal; font-family:verdana, helvetica; font-size:12pt; color:black">
<xsl:apply-templates/>
</span>
<br/>
</xsl:template>
<xsl:template match="channel/link">
<span style="font-weight:normal; color:blue">
<a>
<xsl:attribute name="href"><xsl:value-of select="." /></xsl:attribute>
link
</a>
</span>
<br/>
</xsl:template>
<xsl:template match="channel/webMaster">
<span style="font-weight:normal; font-style:italic; font-family:serif; font-size:10pt; color:black">
Webmaster - <xsl:apply-templates/>
</span>
<br/>
<hr/>
</xsl:template>
<xsl:template match="channel/language">
<span style="display:none">
<xsl:apply-templates/>
</span>
</xsl:template>
<xsl:template match="channel/item">
<xsl:for-each select="title">
<span style="font-weight:bold; font-family:serif font-size:12pt; color:red">
<xsl:apply-templates/>
</span>
<br/>
</xsl:for-each>
<xsl:for-each select="description">
<span style="font-weight:normal; font-family:verdana, helvetica; font-size:10pt; color:black">
<xsl:apply-templates/>
</span>
<br/>
</xsl:for-each>
<xsl:for-each select="link">
<span style="font-weight:normal; color:blue">
<a>
<xsl:attribute name="href"><xsl:value-of select="." /></xsl:attribute>
link
</a>
</span>
<br/>
</xsl:for-each>
<br/>
</xsl:template>
</xsl:stylesheet>
brothercake
02-07-2003, 01:16 PM
Originally posted by mpjbrennan
Here's a very quick attempt - I'm sure it can be improved upon. For example, I haven't figured out yet how to deal with all the < and > url's in the description paragraphs.
Could you not just dump the whole node into a paragraph - the entities will appear as < > in the final thing, thereby turning into links by themselves ..?
mpjbrennan
02-07-2003, 01:18 PM
That's what my stylesheet does - the links appears as <a></a> combinations but they don't have href attributes and don't respond to the mouse.
patrick
mpjbrennan
02-07-2003, 03:32 PM
Here's a tidied-up version - but I still can't figure out those > < entities!
patrick
------------------
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<title>RSS feed</title>
</head>
<body>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template match="channel">
<span style="font-weight:bold; font-family:serif; font-size:26pt; color:green">
<xsl:value-of select="title" />
</span>
> > > >
<span style="font-weight:normal; font-family:verdana, helvetica; font-size:12pt; color:black">
<xsl:value-of select="description" />
</span>
<br/>
<span style="font-weight:normal; font-family:verdana, helvetica; font-size:10pt; color:black">
Contact the Webmaster - <a>
<xsl:attribute name="href">mailTo:<xsl:value-of select='webMaster'/></xsl:attribute>
<xsl:value-of select='webMaster'/>
</a>
</span>
<span style="font-weight:normal; font-family:verdana, helvetica; font-size:10pt; color:blue">
<a>
<xsl:attribute name="href"><xsl:value-of select="link" /></xsl:attribute>
visit the Foundation's site</a>
</span>
<br/>
<span style="display:none">
<xsl:value-of select="language"/>
</span>
<hr/>
<xsl:for-each select="item">
<span style="font-weight:bold; font-family:serif font-size:12pt; color:red">
<xsl:value-of select="title"/>
</span>
<br/>
<span style="font-weight:normal; width:90%; font-family:verdana, helvetica; font-size:10pt; color:black">
<xsl:value-of select="description"/>
</span>
<br/>
<span style="font-weight:normal; color:blue">
<a>
<xsl:attribute name="href"><xsl:value-of select="link" /></xsl:attribute>
Link to source
</a>
</span>
<br/>
<br/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
head8k
02-07-2003, 10:33 PM
Thanks guys I will try this out at work when I'm back in on Tuesday.
I can see why the hyperlinks would be a problem. Strictly speaking they shouldn't be included within the description tag but in my experience people always bung whatever they like into an XML document without considering the consequences!
mpjbrennan
02-08-2003, 04:55 AM
You might find another problem with the description tags - the author has bunged in some unclosed <p> tags.
patrick