RSS谈

Posted on 2007-12-07 21:32 yukui 阅读(326) 评论(0)  编辑  收藏 所属分类: 技术

一、基本概念:

“忽如一夜春风来,千树万树梨花开”,近来RSS的概念已然流行开来,那么,什么才是RSS呢?怎样才能实现RSS的功能呢?以下是手艺人对RSS的理解。

RSS的概念:

以下是某知名网站对RSS的标准描述:
RSS是站点用来和其他站点之间共享内容的一种简易方式(也叫聚合内容),通常被用于新闻和其他按顺序排列的网站,例如Blog。一段项目的介绍可能包含新闻的全部介绍等。或者仅仅是额外的内容或者简短的介绍。这些项目的链接通常都能链接到全部的内容。网络用户可以在客户端借助于支持RSS的新闻聚合工具软件,在不打开网站内容页面的情况下阅读支持RSS输出的网站内容。说得更加简单一点,RSS就是一种用来分发和汇集网页内容的XML格式!

注意最后一句,明白了吗?RSS实质就是一个特殊格式的XML文件

RSS的过程:
1、服务器提供RSS服务,实际上就是生成一个RSS格式的XML文件。
2、RSS阅读器接收这个RSS格式的XML文件并解释他的内容,然后将他显示出来。其显示的内容相当于一个目录表,这个目录表中包含有到原文的超级链接。
3、用户点击RSS文件中的链接,浏览器显示出全文。

如果你用过RSS,你会发现他实际上不过是一个导航的目录表而已,你订阅了一个RSS,其实质就是从网站上接收了一个RSS格式的XML文件,然后,你的RSS阅读器就解释这个XML文件,然后(总是有然后)显示出来,当然,在这个过程中,阅读器也许会将其转换为HTML格式文件(如Maxsthon就是这样处理的。)说到底,XML不过是一种保存数据的方式,对数据含义的解释和使用最终都是应用软件的事。


二、RSS的文件格式:

从上文可以看出,RSS技术的关键在于RSS格式的XML文件,正是这个XML文件沟通了服务器端和客户机端,S和C端通过这个XML文件进行对话:S端按固定的格式生成XML文档,C端接收这个文档并解释之,然后显示出来。

RSS具有固定的格式,当然,由于历史和版本的原因,RSS的XML文件格式具有好几种,以下是常见的几种:

Example  Of RSS

Figure 1 Sample RSS 1.0 Document

<rdf:RDF
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns="http://purl.org/rss/1.0/"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
>
  <channel rdf:about="http://skonnard.com/blog/rss.xml">
    <title>The XML Files</title>
    <link>http://skonnard.com/blog</link>
    <description>by Aaron Skonnard</description>
    <image rdf:resource="http://skonnard.com/blog/images/image.gif" />
    <items>
      <rdf:Seq>
        <rdf:li resource=" http://skonnard.com/blog/entry1" />
        <rdf:li resource=" http://skonnard.com/blog/entry2" />
      </rdf:Seq>
    </items>
  </channel>
  <image rdf:about=" http://skonnard.com/blog/images/image.gif">
    <title>skonnard.com</title>
    <link>http://skonnard.com/blog</link>
    <url>http://skonnard.com/blog/images/image.gif</url>
  </image>
  <item rdf:about="http://skonnard.com/blog/entry1">
    <title>1st blog entry</title>
    <link>http://skonnard.com/blog/entry1</link>
    <description>This is my first blog entry.</description>
    <dc:date>2004-01-13T17:16:44.9803903-07:00</dc:date>
  </item>
  <item rdf:about="http://skonnard.com/blog/entry1">
    <title>2nd Blog Entry</title>
    <link>http://skonnard.com/blog/entry1</link>
    <description>This is my second blog entry.</description>
    <dc:date>2004-01-13T17:16:45.9803903-07:00</dc:date>
  </item>
</rdf:RDF>


--------------------------------------------------------------------------------
Figure 2 Sample RSS 2.0 Document

<rss version="2.0">
  <channel>
    <title>The XML Files</title>
    <link>http://Skonnard.com/blog</link>
    <description>by Aaron Skonnard</description>
    <image>
      <url>http://skonnard.com/blog/images/image.gif</url>
      <title>skonnard.com</title>
      <link>http://skonnard.com/blog/</link>
    </image>
    <item>         
      <title>1st blog entry</title>
      <link>http://skonnard.com/blog/entry1</link>
      <description>This is my first blog entry.</description>
      <pubDate>Wed, 14 Jan 2004 17:16:44 GMT</pubDate>
    </item>
    <item>
      <title>2nd blog entry</title>
      <link>http://skonnard.com/blog/entry1</link>
      <description>This is my second blog entry</description>
      <pubDate>Wed, 14 Jan 2004 17:16:45 GMT</pubDate>
    </item>
  </channel>
</rss>


--------------------------------------------------------------------------------
Figure 3 Sample Atom 0.3 Feed

<feed version="0.3" xml:lang="en-us"
  xmlns="http://purl.org/atom/ns#"
>
  <title>The XML Files</title>
  <link>http://skonnard.com/blog/</link>
  <modified>2004-01-13T17:16:45.0004199-07:00</modified>
  <tagline>by Aaron Skonnard</tagline>
  <author>
    <name>Aaron Skonnard</name>
  </author>
  <entry>
    <title>1st blog entry</title>
    <link>http://skonnard.com/blog/entry1</link>
    <created>2004-01-13T17:16:44.9803903-07:00</created>
    <content type="text/html" mode="xml">
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>This is my first blog entry</p>
      </body>
    </content>
  </entry>
  <entry>
    <title>2nd blog entry</title>
    <link>http://skonnard.com/blog/entry2</link>
    <created>2004-01-13T17:16:45.9803903-07:00</created>
    <content type="text/html" mode="xml">
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>This is my second blog entry</p>
      </body>
    </content>
  </entry>
</feed>


--------------------------------------------------------------------------------
Figure 4 Sample Blogroll (OPML)

<opml>
  <head> <title>Aaron's Favorite Blogs</title> </head>
  <body>
    <outline type="rss" title="PDC Bloggers"
      description="PDC Bloggers website"
      xmlUrl="http://pdcbloggers.net/Feed.rss"
      htmlUrl="http://PDCBloggers.net" />
    <outline type="rss" title="MSDN Magazine: Current Issue"
      description="The Microsoft Journal for Developers"
      xmlUrl="http://msdn.microsoft.com/msdnmag/rss/recent.xml"
      htmlUrl="http://msdn.microsoft.com/msdnmag/" />
    <outline type="rss" title="MSDN Just Published"
      description="Keep current ..."
      xmlUrl="http://msdn.microsoft.com/rss.xml
      htmlUrl="http://msdn.microsoft.com/" />   
  </body>
</opml>


--------------------------------------------------------------------------------
Figure 5 Generating an RSS 2.0 Feed in ASP.NET

<%@ Page language="c#" Codebehind="rss.aspx.cs"
    AutoEventWireup="false" Inherits="SimpleBlog.rss" %>
<rss version="2.0">
  <channel>
    <title>My Blog</title>       
    <link>http://localhost/simpleblog/default.aspx</link>
    <description>A weblog about nothing...</description>
    <language>en-us</language>
    <asp:Repeater id="Items" runat="server">
      <ItemTemplate>
        <item>
        <title><%#DataBinder.Eval(Container.DataItem,
            "title")%></title>
        <description><%#DataBinder.Eval(
            Container.DataItem,"description")%></description>
        <pubDate><%#DataBinder.Eval(Container.DataItem,
            "pubdate") %></pubDate>
        <link><%# DataBinder.Eval(Container.DataItem, "link") %></link>
        </item>
      </ItemTemplate>
    </asp:Repeater>
  </channel>
</rss>


--------------------------------------------------------------------------------
Figure 6 RSS Aggregator Web User Control

<%@ Control Language="c#" AutoEventWireup="true"
    EnableViewState="false" Debug="true"%>
<%@ Import namespace="System.Xml" %>
<%@ OutputCache Duration="1800" VaryByParam="none" %>
<script runat="server" language="C#">
private void Page_Load(object sender, System.EventArgs e)
{
  StringBuilder sb = new StringBuilder();
  XmlDocument doc = new XmlDocument();
  doc.Load(Server.MapPath("blogroll.opml"));
  int numToDisp = int.Parse(doc.SelectSingleNode(
    "/opml/@numberToDisplay").InnerText);
  XmlNodeList rss = doc.SelectNodes("//outline/@xmlUrl");
  foreach (XmlNode r in rss)
  {
    XmlDocument blogdoc = new XmlDocument();
    blogdoc.Load(r.Value);
    XmlNodeList items = blogdoc.SelectNodes("//item"); 
    for (int i=0; i<items.Count && i<numToDisp; i++)
    {
      string author="";
      XmlNode authorNode = items[i].SelectSingleNode(
       "*[local-name()='author' or local-name()='creator']");
      if (authorNode != null) author = authorNode.InnerText;
      sb.Append(String.Format(
        "•&nbsp;<a href={0}>{1} ({2})</a><br/>",
        items[i].SelectSingleNode("link").InnerText,
        items[i].SelectSingleNode("title").InnerText, author));
    }       
  }
  EntriesHTML.Text = sb.ToString();
}
</script>
<style> <!-- styles omitted for brevity --> ...  </style>

<div class="title">UNUG Blogs</div>
<asp:Literal id="EntriesHTML" runat="server"></asp:Literal>

 
以上RSS格式的主要区别在于1.0版RSS和2.0版RSS的区别:
RSS 1.0 和 2.0 格式所包含的核心信息相同,但其结构不一样。我提供了一个 RSS 1.0 文档及一个同等的 RSS 2.0用于对照。
你会注意到顶行右边开始的根元素的差异,RSS 1.0 的根元素是 rdf:RDF,而 RSS 2.0 的根元素是 rss。rss 还包含一个强制版本属性用以表示所用的RSS的准确格式(可能的值包括:0.91, 0.94 等)。另一个主要差别是 RSS 1.0 文档有名字空间限定,RSS 2.0 的文档就没有。不管怎样,包含在两个文档中的信息本质上是一样的。
  两个版本都包含 channel 元素,而 channel 元素又包含三个必须的元素:title、description 和 link,其代码如下:
<channel>
   <title><!-- channel 的标题 --></title>
   <description><!-- 简要描述 --></description>
   <link><!-- channel 的 URL --></link>
   <!-- 可选/可扩展元素 -->
</channel>     
  除了这些必须的元素外,RSS 1.0 还定义了三个附加元素:image、items 和 textinput,其中,image 和 textinput 是可选的。另一方面,RSS 2.0 提供了 16 个附加元素,其中也包括 image、items 和 textinput,此外还有 language、copyright、managingEditor、pubDate 和 category。RSS 1.0 允许通过定义在单独的 XML 名字空间中的可扩展元素来创建这种类型的元数据。
  这两种格式在结构上的主要区别必须要看其 item、image 和 textinput 节点的表示形式。RSS 1.0 中,channel 元素包含对 item、image 和 textinput 节点的引用,这些节点存在于 channel 节点本身之外。这样在 channel 和 所引用的节点之间建立了一种 RDF 关联。如 Figure 1 所示,channel 元素与一个 image 元素以及两个 item 元素关联。RSS 2.0 中,item 元素只是在 channel 元素中连续排放(如 Figure 2 所示)。item 元素包含实际的新闻项信息。item 的结构在两个版本中是相同的。item 元素通常包含 title、link 和 description 元素,如下代码所示:

<item>
   <title><!-- 项标题 --></title>
   <link><!-- 项 URL --></link>
   <description><!-- 简要描述 --></description>
   <!-- 可选的/可扩展的元素 -->
</item>
  在 RSS 1.0 中,title 和 link 是必须的,description 是可选的。而在 RSS 2.0 中,title 或 description 必须提供其中的一个;其它均可选。这些只是定义在 RSS 1.0 中的 item 元素。RSS 2.0 提供几个其它可选元素,其中有 author、category、comments、enclosure、guid、pubDate 和 source。RSS 1.0 获取这样的元数据是通过定义在单独的 XML 名字空间中称为 RSS 模块的可扩展元素来实现的。例如,在 1中,item 的日期是用 Dublic Core 模块的 <dc:date> 元素表示的。
三、知道这些,我们能够做什么?

现在,你知道RSS是怎么回事了吗?其实当你理解了RSS,你会觉得他非常简单:
知道了这些内容,我们至少可以做两方面的事:
1、按照上面h提供的RSS文档格式编写一个XML的文档xxx.xml,将这个文档放到你的网站上,
打开你的rss阅读器,将这个xml的URL地址输入RSS订阅的URL栏,然后点击“确定”,你看到了什么?你的RSS阅读器中已经显示出了你所编辑的内容。
原来你已经提供了RSS服务,一切就是这样简单,下面的内容就是怎样将你网站的内容制成RSS格式的XML文件了,在这个过程中你会用上一些技术,这不属于本文的范畴。
2、编写一个html文档,<body>和</body>之间写上一个<body onload="initrss();"><div id="RSSDIV"></div></body>,
然后,写一段javascript程序的函数initrss(),这个函数将从你的网站上接收xml文档(有多种方法,如使用xmldoc技术就行),然后解释这个xml文档,最简单也是最基本的,你须要析出文档名(即item中的title项)和他的链接(即item中的link项), 然后为这个文档名加上超级链接:htmlcode=<a href="item的link项">item的link项</a>,最后,当然会有最后,不是吗?将这一段html代码写入一个<div></div>中:RSSDIV.innerHTML=htmlcode;,保存这个html文档,用你的IE浏览器打开它,你看到了什么?你已经制成了一个简单的rss阅读器了,就这样简单吗?是的,一切就这样简单。而简单总是美好的。



只有注册用户登录后才能发表评论。


网站导航:
 

posts - 131, comments - 12, trackbacks - 0, articles - 32

Copyright © yukui