Secrets of RSS

RSS version 1.0 was quite a departure from RSS 0.91. Here's what our sample RSS 0.91 document looks like in RSS 1.0.

<?xml version="1.0"?> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" > <channel rdf:about="http://www.rssmaniac.com/steve"> <title>Steve's News</title> <link>http://www.rssmaniac.com/steve</link> <description> This feed contains news from Steve! </description> <image rdf:resource="http://www.rssmaniac.com/steve/ Image.jpg" /> <items> <rdf:Seq> <rdf:li resource= "http://xml.com/pub/2000/08/09/xslt/xslt.html" /> </rdf:Seq> </items> </channel> <image rdf:about="http://www.rssmaniac.com/steve/Image.jpg"> <title>Steve's News</title> <link>http://www.rssmaniac.com/steve</link> <url>http://www.rssmaniac.com/steve/Image.jpg</url> </image> <item rdf:about="http://www.rssmaniac.com/steve/about.html"> <title>Steve shovels the snow</title> <link>http://www.rssmaniac.com/steve</link> <description> It snowed once again. Time to shovel! </description> </item> </rdf:RDF>

You can see this document opened in RSSReader (Figure 4.2).

Figure 4.2. Here's our sample RSS 1.0 document opened in RSSReader.

As you see, an RSS 1.0 document varies greatly from an RSS 0.91 document. For one, the organization of the data is different: the <channel> element is closed before you list the <item> elements, for example. And you list a summary of the items in an <items> element, in <rdf:li> elements. For that matter, the document element itself is differentit begins with <rdf:RDF>, not <rss>. The very name of the <rdf:RDF> element emphasizes RSS 1.0's connection to RDF, the Resource Description Format.

But why do some element names start with "rdf:"? That prefix represents a namespace in XML. Because you make up your own element names in XML, there's a good chance a name you make up may conflict with a name made up by someone else, so namespaces were introduced. Namespaces correspond to prefixes in XML documents. So, even if you have an element named <address> and someone else uses the same element name, you can use a namespace prefix such as <mine:address>, and they can use a different namespace prefix like <theirs:address>, and the elements won't conflict.

You set up a namespace in the document element by associating it with a unique text string (because your text string is unique, the elements in your namespace will be different and so you will have no conflict). A URL is usually used as this unique text string, because a Web resource has to have a unique URL. The URL can be your home page, for example.

In the <rdf:RDF> document element, an RSS 1.0 document connects the prefix rdf with the standard set of RDF elements, and sets up a default namespace for the rest of the document. (If you omit the prefix in an element, RSS readers will assume that element is in the default namespace.)

<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" >

Now that you've used the <rdf:RDF> element, if you omit any prefix, your elements are assumed to be in the default namespace, which corresponds to the RSS 1.0 namespace (which in our example has been defined to correspond to the URL http://purl.org/rss/1.0/). When you use namespaces, your XML elements won't conflict with anyone's, and that namespace has a distinct advantageyou can create and add your own XML elements to RSS 1.0 documents. All you have to do is define a new namespace prefix; then you can make up and add your own elements to the document, as long as you preface them with the new prefix. For example, you might add the date to various elements, showing when they were created.

<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:steve="http://www.rssmaniac.com/steve/about.html" xmlns="http://purl.org/rss/1.0/" > <channel rdf:about="http://www.rssmaniac.com/steve"> <steve:date>2005-03-01</steve:date> <title>Steve's News</title> <link>http://www.rssmaniac.com/steve</link> <description> This feed contains news from Steve! </description> <image rdf:resource="http://www.rssmaniac.com/steve/ Image.jpg" /> <items> <rdf:Seq> <rdf:li resource= "http://xml.com/pub/2000/08/09/xslt/xslt.html" /> </rdf:Seq> </items> </channel> <image rdf:about="http://www.rssmaniac.com/steve/Image.jpg"> <title>Steve's News</title> <link>http://www.rssmaniac.com/steve</link> <url>http://www.rssmaniac.com/steve/Image.jpg</url> </image> <item rdf:about="http://www.rssmaniac.com/steve/about.html"> <title>Steve shovels the snow</title> <steve:date>2006-09-01</steve:date> <link>http://www.rssmaniac.com/steve</link> <description> It snowed once again. Time to shovel! </description> </item> </rdf:RDF>

In fact, there is an existing extension to RDF named the Dublin Core, and it already has many built-in elements you can add to RDF, including a <date> element. So when you give the Dublin Core elements their usual prefix of dc, and you connect that prefix to the defined Dublin Core namespace (which is "http://purl.org/dc/elements/1.1/"), you can add dates to any element in RSS 1.0.

<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns="http://purl.org/rss/1.0/" > <channel rdf:about="http://www.rssmaniac.com/steve"> <dc:date>2005-03-01</steve:date> <title>Steve's News</title> <link>http://www.rssmaniac.com/steve</link> <description> This feed contains news from Steve! </description> <image rdf:resource="http://www.rssmaniac.com/steve/ Image.jpg" /> <items> <rdf:Seq> <rdf:li resource= "http://xml.com/pub/2000/08/09/xslt/xslt.html" /> </rdf:Seq> </items> </channel> <image rdf:about="http://www.rssmaniac.com/steve/Image.jpg"> <title>Steve's News</title> <link>http://www.rssmaniac.com/steve</link> <url>http://www.rssmaniac.com/steve/Image.jpg</url> </image> <item rdf:about="http://www.rssmaniac.com/steve/about.html"> <title>Steve shovels the snow</title> <dc:date>2006-09-01</steve:date> <link>http://www.rssmaniac.com/steve</link> <description> It snowed once again. Time to shovel! </description> </item> </rdf:RDF>

As you can see, RSS 1.0 is based heavily on RDFnot the way the most popular version, RSS 2.0, was written. Many RSS authors wanted to reclaim RSS as its own language, not as an RDF sublanguage.

Категории