XML

Inside the RSS 2.0 Language

It's time to get down to the business of sorting out exactly how XML fits into the RSS 2.0 picture. As you know by now, RSS 2.0 is an XML markup language used to syndicate web content. You may be pleasantly surprised by the relative simplicity of the RSS 2.0 language. When it comes right down to it, there isn't all that much information associated with an RSS news feed, and this simplicity translates directly over to the RSS language.

For the sake of brevity, from here on when I mention RSS, please assume that I'm referring to RSS 2.0.

Before getting any deeper into the RSS language, it's important to clarify a few terms. An RSS news item is a discrete piece of content that ultimately resolves to a specific link to a resource on a web site. A collection of related news items can be gathered together into a channel. As an example, if you wanted to divide your news feeds into three categories for Entertainment, Sports, and Technology, each of these categories would constitute a channel. Each piece of syndicated content (each story) within a given category constitutes a news item. An RSS document is broken down into a channel that contains news items, as shown in Figure 24.3.

Figure 24.3. An RSS document consists of a single channel containing multiple items.

Notice in the figure how each document is limited to a single channel that contains multiple items. There is a lot of flexibility in terms of how many items you assign to a channel even though you can only specify one channel per document.

Getting back to the RSS language, any news item can be described using only a few pieces of information:

  • Title The title of the news item

  • Description A brief one or two sentence description of the item

  • Link The URL of the web resource associated with the item

  • Publication Date The publication date of the news item

These conceptual item descriptors translate directly to the RSS language. More specifically, a news item in RSS is coded using <title>, <description>, <link>, and <pubDate> tags. The following is an example of how these tags are used to code a real news item:

<title>My Car Has a Virus!</title>
<description>This is a little disturbing but I suppose it was inevitable
wireless networking technologies are now potentially opening up automobiles to
computer viruses.</description>
<link>http://www.xyz.com/mambo/index.php?option=
com_content&amp;task=view&amp;id=229&amp;Itemid=37</link>
<pubDate>Tue, 02 Aug 2005 10:20:44 CST</pubDate>

The only tags absolutely required for a news item are <title> and <description>. If you leave out the <link> tag, the description of the item serves as its content. There are several other optional tags that you can use to provide further details about a news item, such as <author>, <enclosure>, and <guid> to name a few.

As you can see, the news item flows directly from the four tags I just mentioned. But these tags don't tell the entire story. All this code shows is a snippet of code for a specific news item. What you don't see is how that item fits into a channel, as well as how that channel fits into an entire RSS document. A few more tags are required to complete the equation. More specifically, the <item> and <channel> tags are used to further add structure to an RSS document.

The <item> tag represents a single news item, and in fact, encloses the four item descriptor tags you just learned about. So, you can add the enclosing <item> tag to expand the previous news item listing to look like this:

<item>
  <title>My Car Has a Virus!</title>
  <description>This is a little disturbing but I suppose it was inevitable
  wireless networking technologies are now potentially opening up
  automobiles to computer viruses.</description>
  <link>http://www.xyz.com/mambo/index.php?option=
  com_content&amp;task=view&amp;id=229&amp;Itemid=37</link>
  <pubDate>Tue, 02 Aug 2005 10:20:44 CST</pubDate>
</item>

Nothing changed in the code other than the <title>, <description>, <link>, and <pubDate> tags being placed as children of the <item> tag. You can probably now guess that each individual news item within an RSS document takes the form of this code. But you still haven't found out how these items relate to a channel.

The <channel> tag is used to code the channel in an RSS document. The <channel> tag consists of several child tags that are used to describe the channel, followed by one or more child <item> tags that describe the individual news items. The tags used to describe a channel are actually the same as those used to describe a news item, minus the <pubDate> tag: <title>, <description>, and <link>. In this case, the tags are describing the channel itself, as opposed to a specific news item. Following is an example of a channel coded with these tags:

<channel>
  <title>My Blog</title>
  <description>Technology, entertainment, culture, you name it...</description>
  <link>http://www.xyz.com/</link>
  ...
<channel>

The only tags absolutely required for a channel are <title>, <description>, and <link>. There are several other optional tags that you can use to provide further details about the channel, such as <copyright>, <language>, and <image> to name a few.

What this example doesn't show is how the news items themselves (<items>) are contained within the <channel> tag. You see an entire RSS document come together in the next section. For now, there is one more tag to address: the <rss> tag. The <rss> tag is used to mark up the root element of RSS documents. Along with enclosing the rest of the content in the document, the <rss> tag also serves to identify the version of the RSS feed via the version attribute. In the case of RSS 2.0, you set this attribute to 2.0, as this example shows:

<rss version="2.0">
  ...
</rss>

You've now seen all of the pieces and parts that go into a basic RSS document. The next section pulls together what you've learned about the RSS language and guides you through the creation of a complete RSS document that contains several news feeds.

Back in Access Your ITunes Music Library Via XML, "Access Your iTunes Music Library via XML," you learned about XML's role in Apple's iTunes digital music service. RSS factors heavily into iTunes when publishing podcasts to the iTunes online service. There is an entire set of iTunes-specific tags that you use to code podcast RSS feeds. <itunes:author>, <itunes:summary>, and <itunes:duration> are examples of some of the tags you must include when coding a podcast as a news feed. To learn more, visit Apple's online podcast publishing tutorial at http://phobos.apple.com/static/iTunesRSS.html.


by BrainBellupdated
Advertisement: