RSS订阅是我们常用到的工具,查看RSS的源码会发现RSS其实是一种XML文档,利用IE直接打开没有转换修饰过的RSS会看打树形结构的XML文档。这种树形结构对于阅读来说,未必就是最合适的,这时候我们就可以利用XSLT把XML转化成为更适合阅读的HTML。Feedsky等烧录过的RSS可以更换不同的模板,便是利用XSLT实现的。
XSLT是扩展样式表转换语言(Extensible Stylesheet Language Transformations)的简称,这是一种对XML文档进行转化的语言,XSLT中的T代表英语中的“转换”(transformation)。它是XSL(Extensible stylesheet language)规范的一部分,XSL规范的另外一部分是XSL-FO(FO代表格式化对象Formatting Objects)。
XSLT是把XML文档转化为另一文档的转换语言,即将源文档的所有数据或者部分数据,利用XPath进行选择,生成另外的XML文档或者其他可直接显示或打印的文件格式(例如 HTML文件、RTF文件或者TeX文件)。如果说CSS是用来修饰HTML的,那么XSLT便是用来修饰XML的。
XSLT语言是声明性的语言,即XSLT程序本身只是包含了一些转换规则的文档。而这些规则可以被递归地应用到转换过程中。XSLT处理程序会首先确定使用XSLT中的哪些规则,然后根据优先级作出相应的转换操作。XSLT本身也是一份XML文档,所以它也必须遵守严格的XML规范。
下面是我到RSS中使用的XSLT文件源码(修改自feedsky模板)
- <?xml version="1.0" encoding="UTF-8"?>
- <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
- <xsl:output method="html" indent="yes" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"/>
- <xsl:variable name="title" select="/rss/channel/title"/>
- <xsl:variable name="feedUrl" select="/rss/channel/atom:link[@ref='self']/@href" xmlns:atom="http://www.w3.org/2005/Atom"/>
- <xsl:variable name="selfFeedUrl" select="/rss/channel/fs:self_link/@href" xmlns:fs="http://www.feedsky.com/namespace/feed"/>
- <xsl:variable name="srclink" select="/rss/channel/link"/>
- <xsl:template match="/">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
- <title>
- <xsl:value-of select="$title"/> - powered by Wolfstyle</title>
- <link rel="stylesheet" href="/xslt/ft5.css" type="text/css"/>
- <link rel="alternate" type="application/rss+xml" title="RSS" href="http://feed.feedsky.com/wolfit"/>
- <xsl:element name="script">
- <xsl:attribute name="type">text/javascript</xsl:attribute>
- <xsl:attribute name="src">/xslt/xsl.js</xsl:attribute>
- </xsl:element>
- <xsl:element name="script">
- <xsl:attribute name="type">text/javascript</xsl:attribute>
- <xsl:attribute name="src">/xslt/tip.js</xsl:attribute>
- </xsl:element>
- <xsl:element name="script">
- <xsl:attribute name="type">text/javascript</xsl:attribute>
- <xsl:attribute name="src">/xslt/common.js</xsl:attribute>
- </xsl:element>
- <xsl:element name="base">
- <xsl:attribute name="href"><xsl:value-of select="$srclink"/></xsl:attribute>
- </xsl:element>
- </head>
- <xsl:apply-templates select="rss/channel"/>
- </html>
- </xsl:template>
- <xsl:template match="channel">
- <body onload="go_decoding();">
- <DIV id="TipLayer" style="visibility:hidden;position:absolute;z-index:1000;top:-100;text-align:left;font-size:12px;"/>
- <div id="header">
- <h1><a href="{link}" style="text-decoration:none;"><xsl:value-of select="$title"/></a></h1>
- <p id="desc"><xsl:value-of select="description" disable-output-escaping="yes"/></p>
- </div>
- <div id="navigator">
- <ul>
- <li><strong>Feed订阅:</strong>这是一个可以使用RSS阅读器进行订阅的页面,您可以将这个地址添加到您习惯使用的RSS阅读器中。</li>
- <li><strong>在线订阅:</strong>下面列出了常用的几款在线RSS阅读器的快捷订阅图标,您可以直接点击进行订阅。</li>
- <li>
- <a href="http://www.xianguo.com/subscribe.php?url=http://feed.feedsky.com/wolfit">
- <img src="http://img.feedsky.com/images/icon_subshot02_xianguo.jpg" border="0" alt="订阅到鲜果"/></a>
- <a href="http://www.zhuaxia.com/add_channel.php?url=http://feed.feedsky.com/wolfit">
- <img src="http://img.feedsky.com/images/icon_subshot02_zhuaxia.gif" border="0" alt="订阅到抓虾"/></a>
- <a href="http://www.pageflakes.com/subscribe.aspx?url=http://feed.feedsky.com/wolfit">
- <img src="http://img.feedsky.com/images/icon_subshot02_pageflakes.gif" border="0" alt="订阅到飞鸽"/></a>
- <a href="http://www.netvibes.com/subscribe.php?url=http://feed.feedsky.com/wolfit">
- <img src="http://img.feedsky.com/images/icon_subshot02_netvibes.gif" border="0" alt="netvibes"/></a>
- <a href="http://fusion.google.com/add?feedurl=http://feed.feedsky.com/wolfit">
- <img src="http://img.feedsky.com/images/icon_subshot02_google.gif" border="0" alt="google reader"/></a>
- <a href="http://add.my.yahoo.com/rss?url=http://feed.feedsky.com/wolfit">
- <img src="http://img.feedsky.com/images/icon_subshot02_yahoo.gif" border="0" alt="my yahoo"/></a>
- <a href="http://www.rojo.com/add-subscription?resource=http://feed.feedsky.com/wolfit">
- <img src="http://img.feedsky.com/images/icon_subshot02_rojo.gif" border="0" alt="Rojo"/></a>
- <a href="http://www.newsgator.com/ngs/subscriber/subfext.aspx?url=http://feed.feedsky.com/wolfit">
- <img src="http://img.feedsky.com/images/icon_subshot02_newsgator.gif" border="0" alt="Newsgator"/></a>
- <a href="http://www.bloglines.com/sub/http://feed.feedsky.com/wolfit">
- <img src="http://img.feedsky.com/images/icon_subshot02_bloglines.gif" border="0" alt="bloglines"/></a>
- <a href="http://reader.yodao.com/#url=http://feed.feedsky.com/wolfit">
- <img src="http://img.feedsky.com/images/icon_subshot02_youdao.gif" border="0" alt="订阅到有道"/></a>
- <a href="http://inezha.com/add?url=http://feed.feedsky.com/wolfit">
- <img src="http://www.wilf.cn/image/wz/icon_subshot02_inezha.jpg" border="0" alt="Subscribe by Anothr"/></a>
- <a href="http://www.feedsky.com/msub_wr.html?burl=wolfit">
- <img src="http://www.wilf.cn/image/wz/icon_subshot02_email.gif" border="0" alt="Subscribe by Anothr"/></a>
- </li>
- <li><strong> 手机阅读:</strong><label id="feedurl" value="{$selfFeedUrl}" value_2="http://feed.feedsky.com/wolfit" style="font-weight:bold;" onMouseOver="stm(Text[0],Style[0])" onMouseOut="htm()"/> <script type="text/javascript" language="JavaScript">
- function wrapURL(){
- var sp=document.getElementById("feedurl");
- var wrapUrl=sp.getAttribute("value");
- if(wrapUrl==""){
- wrapUrl=sp.getAttribute("value_2");;
- }
- if(wrapUrl.toLowerCase().substring(0,7)=="http://")
- {wrapUrlwrapUrl = wrapUrl.substring(7);}
- var arr=new Array();
- arr=wrapUrl.split('/');
- var p="";
- if(arr.length==1)
- {p=arr[0];
- }else if(arr.length==2)
- {p=arr[1];
- }else{
- p=wrapUrl.substring(wrapUrl.indexOf("/")+1);}
- var textNode=document.createTextNode("http://wap.feedsky.com/"+p);
- sp.appendChild(textNode);
- return true;}
- wrapURL();</script>
- <a href="http://feed.feedsky.com/wolfit" title="简单生活" target="_blank"><img src="http://www.feedsky.com/feed/wolfit/sc/orange.gif" style="border:0;float:right;display:inline;" align="middle" alt="" /></a>
- </li>
- <!-- <li><span id="wap">
- <script language="javascript">
- /*M_CR = 0;
- M_SOURCE = "feedsky";
- write_form();
- fillPhone();
- fillSelect(0);
- document.m_form.m_mobile.options.selectedIndex = 0;
- */
- </script>
- </span>
- </li> -->
- </ul>
- </div>
- <div id="main">
- <div id="content">
- <ul id="item">
- <xsl:apply-templates select="item"/>
- </ul>
- <div class="clear"><!-- --></div>
- </div>
- </div>
- <script language="javascript" type="text/javascript">autoSetImgSize("item","98%");</script>
- <div id="copyright">
- <p>@ 2007-2008 <a href="http://www.wilf.cn">Wilf.cn</a><br/>图文版权归本人所有,模版版权归Feedsky所有</p>
- <a href="http://www.wilf.cn"><img src="http://www.wilf.cn/image/logo.gif" border="0"/></a>
- </div>
- </body>
- </xsl:template>
- <xsl:template match="item">
- <li class="regularitem">
- <h3><a href="{link}"><xsl:value-of select="title"/></a></h3>
- <span class="date"> <xsl:value-of select="pubDate"/></span>
- <p name="decodeable" class="itemcontent"><xsl:call-template name="outputContent"/></p>
- <xsl:if test="count(child::enclosure)=1">
- <dd>
- <a href="{enclosure/@url}">
- <img src="http://img.feedsky.com/images/listen.gif" style="vertical-align: middle; padding-left: 4px;"/>
- </a>
- </dd>
- </xsl:if>
- </li>
- </xsl:template>
- <xsl:template match="image">
- <xsl:element name="img" namespace="http://www.w3.org/1999/xhtml">
- <xsl:attribute name="src"><xsl:value-of select="url"/></xsl:attribute>
- <xsl:attribute name="alt">
- Link to <xsl:value-of select="title"/></xsl:attribute>
- <xsl:attribute name="id">feedimage</xsl:attribute>
- </xsl:element>
- <xsl:text> </xsl:text>
- </xsl:template>
- <xsl:template match="feedsky:browserFriendly" xmlns:feedsky="http://namespace.org/feedsky/ext/1.0">
- <p id="ownerblurb" xmlns="http://www.w3.org/1999/xhtml">
- <em>A message from the feed publisher:</em>
- <xsl:text> </xsl:text>
- <xsl:apply-templates/>
- </p>
- </xsl:template>
- <xsl:template name="outputContent">
- <xsl:choose>
- <xsl:when test="xhtml:body" xmlns:xhtml="http://www.w3.org/1999/xhtml">
- <xsl:copy-of select="xhtml:body/*"/>
- </xsl:when>
- <xsl:when test="xhtml:div" xmlns:xhtml="http://www.w3.org/1999/xhtml">
- <xsl:copy-of select="xhtml:div"/>
- </xsl:when>
- <xsl:when test="content:encoded" xmlns:content="http://purl.org/rss/1.0/modules/content/">
- <xsl:value-of select="content:encoded" disable-output-escaping="yes"/>
- </xsl:when>
- <xsl:when test="description">
- <xsl:value-of select="description" disable-output-escaping="yes"/>
- </xsl:when>
- </xsl:choose>
- </xsl:template>
- </xsl:stylesheet>
可以看到,在这个XSLT文件中不仅包含里几个javascript文件和css样式表,更是写入了一些html代码,Z-Blog自带了一个XSLT,如同Z的一贯风格一样简洁。不太满意,于是修改了feedsky到XSLT,也就是上面的代码,效果还不错。
地址在这里:http://www.wilf.cn/rss.xml (IE打开,Firefox就看不到效果了,好像IE下有提示脚本错误,先不管了...)
