Developing Feeds with Rss and Atom
5.6. Creating RSS 1.0 Feeds
Despite the additional complexity of the RDF attributes, the methods for creating RSS 1.0 feeds are similar to those used to create RSS 2.0 feeds (discussed in Chapter 4). 5.6.1. Creating RSS 1.0 with Perl
The XML::RSS module used in Chapter 4 also works for RSS 1.0, with a few changes to the scripts. The script in Example 5-8 produces the feed shown, in turn, in Example 5-9. Example 5-8. Creating RSS 1.0 with XML::RSS
#!/usr/bin/perl -w use XML::RSS; my $rss = new XML::RSS( version => '1.0' ); $rss->channel( title => "The Title of the Feed", link => "http://www.oreilly.com/example/", description => "The description of the Feed", dc => { date => "2000-08-23T07:00+00:00", subject => "Linux Software", creator => 'scoop@freshmeat.net', publisher => 'scoop@freshmeat.net', rights => "Copyright 1999, Freshmeat.net", language => "en-us", }, ); $rss->image( title => "Oreilly", url => "http://meerkat.oreillynet.com/icons/meerkat-powered.jpg", link => "http://www.oreilly.com/example/", dc => { creator => "G. Raphics (graphics at freshmeat.net)", }, ); $rss->textinput( title => "Search", description => "Search the site", name => "query", link => "http://www.oreilly.com/example/search.cgi" ); $rss->add_item( title => "Example Entry 1", link => "http://www.oreilly.com/example/entry1", description => 'blah blah', dc => { subject => "Software", }, ); $rss->add_item( title => "Example Entry 2", link => "http://www.oreilly.com/example/entry2", description => 'blah blah' ); $rss->add_item( title => "Example Entry 3", link => "http://www.oreilly.com/example/entry3", description => 'blah blah' ); $rss->save("example.rdf"); Example 5-9. The RSS feed produced by Example 5-8
<?xml version="1.0" encoding="UTF-8"?> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:syn="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" > <channel rdf:about="http://www.oreilly.com/example/"> <title>The Title of the Feed</title> <link>http://www.oreilly.com/example/</link> <description>The description of the Feed</description> <dc:language>en-us</dc:language> <dc:rights>Copyright 1999, Freshmeat.net</dc:rights> <dc:date>2000-08-23T07:00+00:00</dc:date> <dc:publisher>scoop@freshmeat.net</dc:publisher> <dc:creator>scoop@freshmeat.net</dc:creator> <dc:subject>Linux Software</dc:subject> <items> <rdf:Seq> <rdf:li rdf:resource="http://www.oreilly.com/example/entry1" /> <rdf:li rdf:resource="http://www.oreilly.com/example/entry2" /> <rdf:li rdf:resource="http://www.oreilly.com/example/entry3" /> </rdf:Seq> </items> <image rdf:resource="http://meerkat.oreillynet.com/icons/meerkat-powered.jpg" /> <textinput rdf:resource="http://www.oreilly.com/example/search.cgi" /> </channel> <image rdf:about="http://meerkat.oreillynet.com/icons/meerkat-powered.jpg"> <title>Oreilly</title> <url>http://meerkat.oreillynet.com/icons/meerkat-powered.jpg</url> <link>http://www.oreilly.com/example/</link> <dc:creator>G. Raphics (graphics at freshmeat.net)</dc:creator> </image> <item rdf:about="http://www.oreilly.com/example/entry1"> <title>Example Entry 1</title> <link>http://www.oreilly.com/example/entry1</link> <description>blah blah</description> <dc:subject>Software</dc:subject> </item> <item rdf:about="http://www.oreilly.com/example/entry2"> <title>Example Entry 2</title> <link>http://www.oreilly.com/example/entry2</link> <description>blah blah</description> </item> <item rdf:about="http://www.oreilly.com/example/entry3"> <title>Example Entry 3</title> <link>http://www.oreilly.com/example/entry3</link> <description>blah blah</description> </item> <textinput rdf:about="http://www.oreilly.com/example/search.cgi"> <title>Search</title> <description>Search the site</description> <name>query</name> <link>http://www.oreilly.com/example/search.cgi</link> </textinput> </rdf:RDF>
The differences between creating RSS 2.0 and RSS 1.0 with XML::RSS are slight. Just make sure you declare the correct version, like so: my $rss = new XML::RSS (version => '1.0');
The module takes care of itself, for the most part. If you use other namespaces, you must surround them with their namespace prefix. In this section, the script adds six elements that are part of the Dublin Core module into the channel section of the feed: $rss->channel( title => "The Title of the Feed", link => "http://www.oreilly.com/example/", description => "The description of the Feed", dc => { date => "2000-08-23T07:00+00:00", subject => "Linux Software", creator => 'scoop@freshmeat.net', publisher => 'scoop@freshmeat.net', rights => "Copyright 1999, Freshmeat.net", language => "en-us", }, ); XML::RSS comes with built-in support for the Dublin Core, Syndication, and Taxonomy modules. You can easily add support for any other module: $rss->add_module(prefix=>'my', uri=>'http://purl.org/my/rss/module/'); This line does two things. First, it makes the module add the correct namespace declaration to the root element of the document (here, it adds the line xmlns:my=http://purl.org/my/rss/module/, but you should replace the prefix and the URI with the correct ones for your module). Second, it allows you to use the same syntax as the preceding Dublin Core example to add your elements to the feed. $rss->channel( title => "The Title of the Feed", link => "http://www.oreilly.com/example/", description => "The description of the Feed", dc => { date => "2000-08-23T07:00+00:00", subject => "Linux Software", creator => 'scoop@freshmeat.net', publisher => 'scoop@freshmeat.net', rights => "Copyright 1999, Freshmeat.net", language => "en-us", }, my => { element => 'value', }, );
The rest of the script is identical to the RSS 2.0 creation script using the same module. 5.6.2. Producing RSS 1.0 with PHP
The FeedCreator class in Chapter 4 can produce RSS 1.0 documents. In fact, it's trivially easy to do so with the code in Chapter 4. Just change the last line in Example 5-10. Example 5-10. A example of PHP code that produces an RSS 1.0 feed
<? include("feedcreator.class.php"); $rss = new UniversalFeedCreator( ); $rss->title = "Example Feed"; $rss->description = "This is the feed description"; $rss->link = "http://www.example.com/"; // Image section $image = new FeedImage( ); $image->title = "example logo"; $image->url = "http://www.example.com/images/logo.gif"; $image->link = "http://www.example.com"; $image->description = "Visit Example.com!"; $rss->image = $image; // Item Loop $item = new FeedItem( ); $item->title = "Entry one"; $item->link = "http://www.example.com/entryone"; $item->description = "This is the content of the first entry"; $rss->addItem($item); // End Item Loop echo $rss->saveFeed("RSS1.0", "news/feed.rdf"); ?>
Once again, to remind you, the methods are named after the RSS elements, so it's easy to see what's happening. There's really very little extra to say that hasn't been covered in Chapter 4: namespaced modules are trickier, and require some extra code. We'll deal with this, and RSS 1.0 modules in great detail, in the next chapter. |