In the previous part we have introduced the basics for the creation of custom RSS feeds. We ended the article creating the xml object and we were ready to write down the feed itself. Please, before going on here, see the first article if you haven't, otherwise you won't understand the following explanations.
The FeedFirst of all, we start creating the head of the feed. Just for explanatory purpose, let me say that a feed needs some basic elements:
1) a version;
2) a title;
3) a ttl (time to live);
4) a language;
5) if you like, a copyright statement;
6) a publishing date.
Let's create the first part of the feed with those information, which are someway general and not the news items themselves.
After the creation of the filesystemobject, add the following code:
All the parts in italics should be customized by you. Please change them according to your needs and information. Remember that the above snippet should be pasted just after the code provided in the previous article.
objtstream.writeline "<?xml version="&chr(34)&"1.0"&chr(34)&" encoding="&chr(34)&"UTF-8"&chr(34)&"?>"
objtstream.writeline "<rss version="&chr(34)&"2.0"&chr(34)&">"
objtstream.writeline "<title>Your title</title>"
objtstream.writeline "<description>Your description</description>"
objtstream.writeline "<title>Your title</title><link>http://www.yourdomain.com</link></image>"
objtstream.writeline "<copyright>Your copyright</copyright>"
vardata = return_RFC822_Date(Now(), "GMT")
Let's see what we are doing here.
The first 2 lines are actually stating the feed version. After that we open the channel tag and we put some general information about the feed itself:
1) the title, a general link to your web page and a description of the feed;
2) the ttl (time to live). It specifies the number of minutes the feed can stay cached before refreshing it from the source. We set it to 60;
3) the language is optional. Here you can find the list of rss language codes;
4) an image, another title, link and a copyright - those are completely optional;
5) a last build date and a publication date. In our example, those two dates are the same and they are converted using the RFC822 format date function explained in the previous part of this article. The actual date used is obtained with the asp function Now().
The news itemsAfter creating the heading of our feed we start inserting the news items.
We create a repeat region and set the number of items to be inserted.
The var_count (which we set to 0 at the beginning) will count the items to be inserted into the feed. In the example is set to 15. That means that no more than 15 items will be inserted. Remember that, when you get your data from the database: in fact you should order or filter the recordset according to which items you want to be inserted into the feed (for example order them by date).
while (var_count < 15) and (not rs.eof)
Now let's insert the news items.
We inserted the title and the link. <guid> is optional and it defines a unique identifier for the item. In the example we use the link again but you could use the unique identifier of your table.
With the above line, we set the actual news (the text). Remember that you can style the news text according to your needs adding the appropriate html tags.
We then add the news publication date and close the item tag:
We then close the repeat region, after adding 1 to var_count:
vardatain = return_RFC822_Date(rs.fields.item("news_date").value, "GMT")
Finally we close the channel, the rss and the objectstream:
var_count = var_count + 1
Set objtstream = nothing
Set objfso = nothing
The HTML partThe main part is all above. You can then add a little html section to your generate.asp page to make it more user friendly. Add the following snippet just after the rest of the code:
After that, remember to close your recordset connection.
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<p>Processing Rss Feed creation</p>
When you will run the page, the xml file (news.xml) will be created into the rss folder. Open it and check if everything's ok. Now you have a news RSS feed!
That's all. Hope you find this two parts article useful.
Please share your thoughts in the section below!