首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

使用 Atom 结构避免在 feed 聚合过程中出现数据重复

使用 Atom 结构避免在 feed 聚合过程中出现数据重复

关于 feedFeed 用于联合新闻归档或类似于 blog 的 Web 应用程序中的数据。本例中的 feed(如清单 1 所示)具有潜在的问题,其条目的 id 有可能在别处产生重复。如果此重复项未覆盖当前的条目,那么将在联合中创建重复的内容。
清单 1. 原始 feed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns='http://www.w3.org/2005/Atom'
      xmlnspenSearch='http://a9.com/-/spec/opensearchrss/1.0/'>
<id>http://www.example.com/myblog/index.html&lt;/id>
<updated>2007-01-10T17:23:36.222-08:00</updated>
<title type='text'>MyBlog updates</title>
<link rel='alternate' type='text/html'
    href='http://www.example.com/myblog/index.html'></link>
<link rel='next' type='application/atom+xml'
     
href='http://www.example.com/myblog/feeds/posts/default?start-index=26&max-results=2
5'></link>
<link rel='self' type='application/atom+xml'
    href='http://www.example.com/myblog/feeds/posts/default'></link>
<author><name>tsa</name></author>
<generator version='1.01' uri='http://www.example.com'>Example
Blogerator</generator>
<openSearch:totalResults>2</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<entry>
<id>http://www.example.com/myblog/posts/55&lt;/id>
<published>2007-01-10T17:21:00.000-08:00</published>
<updated>2007-01-10T17:23:35.767-08:00</updated>
<title type='text'>My first post ever</title>
<content type='html'>Content goes here!</content>
<link rel='alternate' type='text/html'
    href='http://www.example.com/myblog/posts/55'></link>
<link rel='self' type='application/atom+xml'
    href='http://www.example.com/myblog/feeds/posts/default/55'></link>
<link rel='edit' type='application/atom+xml'
    href='http://www.example.com/myblog/feeds/posts/edit/55'></link>
<author><name>tsa</name></author></entry>
<entry>
<id>http://www.example.com/myblog/posts/56&lt;/id>
<published>2007-01-11T17:21:00.000-08:00</published>
<updated>2007-01-10T17:23:35.767-08:00</updated>
<title type='text'>My second post ever</title>
<content type='html'>Content for second post goes here!</content>
<link rel='alternate' type='text/html'
    href='http://www.example.com/myblog/posts/56'></link>
<link rel='self' type='application/atom+xml'
    href='http://www.example.com/myblog/feeds/posts/default/56'></link>
<link rel='edit' type='application/atom+xml'
    href='http://www.example.com/myblog/feeds/posts/edit/56'></link>
<author><name>tsa</name></author></entry>
</feed>




中的 feed 只包含了两个条目。在本技巧中,您只需关注 id 和 link 标签,以及 Atom feed 的源 URL(包含在link 中)。这是一个有效的 Atom 1.0 feed,但在 id 标签的构造中存在着潜在的隐患。
问题问题的症结就在于使用 URL,或者说是 link 标签的内容,被作为 id 使用。我们必须按照 Atom syndication format 规范以确保惟一性的方式创建 atom:id 元素。(请参阅  部分的链接)。
尽管 blog、新闻 Web 站点或者任何联合内容的结构都有助于防止 id 条目的重复,但是使用 URL 作为 atom:id 元素的内容仍然有产生数据重复的隐患,因此不建议采用这种方式。
比方说,您有可能会编辑 blog 中的一篇帖子或者文章。您保存了新内容后,该内容的 URL 将会与原内容的 URL 一样。然而,这还将创建一条新的数据库条目,也就是说 Atom 条目必须要有一个区别于被更新条目的惟一的 id。
解决之道解决的途径是正确地安排 atom:id 元素的格式。格式化 id 元素的最常用方法就是使用 “标签” URL 方案(请参阅  获得完整的规范)。
从本质上来说,该方案采用的格式可以保证创建 atom:id 元素的过程中不产生数据重复,而且 atom:id 元素仍然可以转化为 URL。
上例中的 3 个 id 元素可以转化为标签方案,如清单 2 所示。
清单 2. 将 id 元素转化为标签方案
1
2
3
<id>tag:example.com,2007:myblog</id>
<id>tag:example.com,2007:myblog.post-55</id>
<id>tag:example.com,2007:myblog.post-56</id>




现在如果您在编辑一个帖子,URL 或者 link 元素将不会发生变化,可是 id 元素可能会变成如下形式: <id>tag:example.com,2007:myblog.post-55.edit-0</id>。URL 仍然可以从 id 元素处获取,这样在编辑完成后也就毫无顾虑了。
这样就完成了! 我希望您不会再因为 atom:id 元素格式安排不当而遇到 feed 条目重复和覆盖的问题。
结束语现在您应该进一步了解了 Atom feed,认识到了保持 atom:id 元素惟一的重要性。更重要的是,在您开发代码创建 feed 时,应牢记本文讨论的思想,确保在 feed 条目中不会出现 atom:id 元素重复的现象。
返回列表