RSS 2.0の作り方

RSSはxmlで記載するので、最初にxmlのバージョン、文字コードを書きます。

[参考記事] サイトの更新情報をPINGサーバに送信する方法
[参考記事] Y!J-BSC/1.0 crawlerの挙動(ページ内のRSSを必ず読みに来る)
[参考記事] サイトマップ(sitemap.xml)のつくり方とちょっとしたテクニック

<?xml version="1.0" encoding="utf-8"?>

それ以降を<html>と同じように、全体を<rss>で囲います。
このタグにはバージョンや仕様も合わせて書きます。

<rss version="2.0"
	xmlns:rss="http://purl.org/rss/1.0/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:atom="http://www.w3.org/2005/Atom">

………

</rss>

<body>と同じように、内容を<channel>で囲います。

<channel>

………

</channel>

まずはこのRSSのタイトルを書きます。

<title>TEST RSS</title>

RSS発行元へのリンクを書きます。

<link>http://www.example.com/news/</link>

RSSの簡単な内容説明を書きます。

<description>ほげほげの最新情報をお届けします!</description>

言語を書きます。

<language>ja</language>

このRSS自体のURLを書きます。(詳しくはちょっと違う)

<atom:link rel="self" href="http://www.example.com/rss.xml" type="application/rss+xml" />

このRSSの最終公開日、最終作成日を書きます。

<pubDate>Tue, 26 May 2009 13:30:32 +0900</pubDate>
<lastBuildDate>Tue, 26 May 2009 13:30:32 +0900</lastBuildDate>

サイトロゴがあれば書きます。
画像は37×37あたりで
Firefoxで見ると表示されます。

<image>
  <title>TEST RSS</title>
  <url>http://www.example.com/logo.gif</url>
  <link>http://www.example.com/news/</link>
</image>

元となる仕様を書きます。

<docs>http://blogs.law.harvard.edu/tech/rss</docs>

各記事

記事ごとにitemで囲います。

<item>

………

</item>

記事のタイトルを書きます。

<title>今日のお知らせ</title>

記事のページURLを書きます。

<link>www.example.com/news/1.html</link>

記事の内容を書きます。ここにはHTMLタグは書けません。HTMLエンティティーなどを書くときはエスケープします。
内容は全角500文字くらいです。

<description>こんにちは。
今日はがんばったよ!</description>

別の書き方で記事の内容を書きます。ここにはHTMLタグが書けます。HTMLエンティティーなどを書くときはエスケープします。

<content:encoded><![CDATA[こんにちは。<lt;br />gt;
今日はがんばったよ!]]></content:encoded>

記事の投稿日を書きます。

<pubDate>Mon, 23 Mar 2009 18:50:19 +0900</pubDate>

HTMLへの書き方

HTML文書の<head>タグ内にリンク先とタイトルを記載します。

<link rel="alternate" type="application/rss+xml" title="新着情報" href="/rss/news.xml" />

HTTPヘッダー

XMLファイルを配布するときは、ファイルの種類や文字コードをHTTPヘッダーで送ります。

Content-Type: text/html; charset=utf-8

サーバで設定するときはhttpd.confまたは.htaccessに次のように書いておきます。

AddType "application/xml; charset=utf-8" .xml

まとめ

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
	xmlns:rss="http://purl.org/rss/1.0/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>TEST RSS</title>
    <link>http://www.example.com/news/</link>
    <description>ほげほげの最新情報をお届けします!</description>
    <language>ja</language>
    <atom:link rel="self" href="http://www.example.com/rss.xml" type="application/rss+xml" />
    <pubDate>Tue, 26 May 2009 13:30:32 +0900</pubDate>
    <lastBuildDate>Tue, 26 May 2009 13:30:32 +0900</lastBuildDate>
    <image>
      <title>TEST RSS</title>
      <url>http://www.example.com/logo.gif</url>
      <link>http://www.example.com/news/</link>
    </image>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <item>
      <title>今日のお知らせ</title>
      <link>www.example.com/news/1.html</link>
      <description>こんにちは。
      今日はがんばったよ!</description>
      <content:encoded><![CDATA[こんにちは。<br />
      今日はがんばったよ!]]></content:encoded>
      <pubDate>Mon, 23 Mar 2009 18:50:19 +0900</pubDate>
    </item>
    <item>
      <title>今日のお知らせ</title>
      <link>www.example.com/news/1.html</link>
      <description>こんにちは。
      今日はがんばったよ!</description>
      <content:encoded><![CDATA[こんにちは。<br />
      今日はがんばったよ!]]></content:encoded>
      <pubDate>Mon, 23 Mar 2009 18:50:19 +0900</pubDate>
    </item>
  </channel>
</rss>

関連記事

スポンサーリンク

crontab プログラムを定期的に実行するcrondの設定ファイルを編集する

ホームページ製作・web系アプリ系の製作案件募集中です。

上に戻る