Click to See Complete Forum and Search --> : RSS Feeds, XML, and date-dependent content?
OO7girl
06-11-2004, 02:06 PM
Hello all,
I'm an XML newbie who's about to dive in and put together an RSS feed for a "today in history" section on a website. I understand XML markup with no problem (I've been coding HTML/CSS for years, so I vaguely know how it works), but I'm not sure how to go about the task at hand.
I have 730 text files - one for every day of the year, in both English and French. I'm hoping to get the RSS feed to call up the correct content for the day of the year that the visitor arrives at my site. In other words, if it's June 11th, the visitor will go to the site and see content specifically tailored to June 11th.
Is this even possible, and should I make 730 RSS feeds/XML files, or 1 huge one?
Any thoughts would be appreciated!
crh3675
06-11-2004, 02:44 PM
I personally would do one per day because it is a lot quicker to sort a file system by date than a series of XML entries.
What programming language are you going to use? What you need is a server-side pre-processing language that produces the feed. For example:
Create a PHP page called "rss.php"
<?php
// Say your files are labeled as such:
// english-2004-06-27.xml
// and they are stored in the data/ folder
$path="data/english-".date("Y-n-d").".xml";
$xml=file_get_contents($path);
header("Content-type:text/xml");
echo $xml;
?>
You could even Javascript the solution within the web page:
<script type="text/javascript">
function loadRss(){
var curDate=new Date();
var dateString=curDate.getFullYear()+"-"+curDate.getMonth()+"-"+curDate.getDate()
var path="data/english-"+dateString+".xml";
document.location.replace(path);
}
</script>
<body onload="loadRss()">
OO7girl
06-14-2004, 07:52 AM
Hi Craig,
I should have mentioned that I can't do anything server-side, so I guess that means no PHP/CGI, etc.
If I inserted your Javascript code on the page that will eventually house the RSS feed, would that result in the RSS displaying the particular "news feed" for a given day?
crh3675
06-14-2004, 07:58 AM
What it would do is push the browser off to that particular XML where the feed is. So, you would create a page and name it somthing like "getrssxml.html" and use the code I provided. Here is the full page:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<title>RSS Feed</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="Expires" content="Mon, 26 Jul 1997 05:00:00 GMT"/>
<meta http-equiv="Cache-Control" content="no-store, no-cache, must-revalidate"/>
<meta http-equiv="Cache-Control" content="post-check=0, pre-check=0"/>
<meta http-equiv="Pragma" content="no-cache" />
<meta name="description" content=""/>
<script type="text/javascript">
//<![CDATA[
function loadRss(){
var curDate=new Date();
var dateString=curDate.getFullYear()+"-"+curDate.getMonth()+"-"+curDate.getDate()
var path="data/english-"+dateString+".xml";
document.location.replace(path);
}
//]]>>
</script>
</head>
<body onload="loadRss()">
</body>
</html>
This script assumes your RSS feed file name would be something like "english-2004-6-14.xml"
OO7girl
06-14-2004, 08:11 AM
Great - thanks!
So I would need to create 730 separate RSS feeds, correct? (Which I could do with some fancy find-and-replace work, I think.)
crh3675
06-14-2004, 08:40 AM
Are the files already in XML format? Can you post a sample file. Maybe I can provide a solution for you if you have that much work to do.
OO7girl
06-14-2004, 08:55 AM
No, they're not already in XML format, but I'm going to have to go through all 730 files one by one anyway, as I need to make some other changes. I'm afraid it's going to be a big job no matter what! Hehe.
I have one more question: How exactly do I call up the RSS feed within the HTML? For example, if I want to place the RSS feed in a table, what code would I need to place the feed in a certain cell? Is this possible?
Thanks again!
crh3675
06-14-2004, 09:05 AM
Are you planning on displaying the XML file by itself or nested within an HTML page? If you want it within an HTML page, you will have to dynamically populate the element or table. You can use an IFRAME which would be easiest. Just include the IFRAME within the table cell:
<td>
<iframe id="rssFeed" src="about:blank" width="100%" height="400" border="1" scrolling="yes"></frame>
</td>
And then, in your BODY tag,
<body onload="loadRss()">
And then, for your javascript:
<script type="text/javascript">
function loadRss(){
var curDate=new Date();
var dateString=curDate.getFullYear()+"-"+curDate.getMonth()+"-"+curDate.getDate()
var path="data/english-"+dateString+".xml";
document.getElementById("rssFeed").src=path;
}
</script>
OO7girl
06-14-2004, 09:16 AM
I was hoping to have the RSS feed nested in a table or something, yes. The only thing is that I was hoping to have it be a very fluid page element that would be as small as the shortest feed and as large as the longest feed without using a scrollbar. I'm not sure whether this is possible with an iframe, but in any case, this is more of a design issue than anything else, and hopefully I can resolve it with some fiddling.
Thanks so much for all your help - it's been wonderful. :)
OO7girl
06-15-2004, 10:37 AM
Hi there,
Sorry to bug you again, but I had a question about the Javascript that you wrote for me and my RSS feed problem. This is the thread:
http://www.webdeveloper.com/forum/s...;threadid=37109
For some reason, the Javascript seems to refer to a page that is one month behind where it should be. For example, today, the script takes me to the XML page /data/english-2004-5-15.xml when it should be taking me to /data/english-2004-6-15.xml.
Unfortunately, I don't know enough about Javascript to fix this problem myself. I've looked at the code and can't quite figure it out! If I name the file for June 15th "english-2004-5-15.xml", then it will display the proper info for today's date, but I think this may confuse other web developers/designers down the line if this "Today in History" feature ever needs to be altered.
Do you have any thoughts? Here is the Javascript you wrote for me:
<script type="text/javascript">
//<![CDATA[
function loadRss(){
var curDate=new Date();
var dateString=curDate.getFullYear()+"-"+curDate.getMonth()+"-"+curDate.getDate()
var path="data/english-"+dateString+".xml";
document.location.replace(path);
}
//]]>>
</script>
And here is the URL for the page that redirects to the proper RSS feed:
http://www25.brinkster.com/oo7girl/vimy/rss3.html
Thanks so much,
Amanda
crh3675
06-15-2004, 10:44 AM
Oops. Javascript interprets the month values from 0-11 not 1-12. So therefore, you need to add 1 to the value of the month.
<script type="text/javascript">
//<![CDATA[
function loadRss(){
var curDate=new Date();
var dateString=curDate.getFullYear()+"-"+(curDate.getMonth()+1)+"-"+curDate.getDate()
var path="data/english-"+dateString+".xml";
document.location.replace(path);
}
//]]>>
</script>
OO7girl
06-15-2004, 11:22 AM
Thanks - that did the trick!!
OO7girl
07-07-2004, 01:59 PM
Hi again Craig,
I've come back to my "Today in History" calendar after working on another project for a bit and have another question for you. Is there some way that I can make the XML files load in an ilayer for Netscape 4.79 users, by combining the iframe and ilayer tags as follows?
<ilayer id="rssFeed_NS" name="rssFeed" width="400px" height="500px">
</ilayer>
<iframe src="" id="rssFeed" name="rssFeed" frameborder="0" scrolling="no"
width="400px" height="500px">
Unfortunately I have to design for Netscape 4.79, even though less than 1% of our site visitors use it.
I have a snippet of code here (I got it from another thread that I started a while ago) that looks like it's on the right track, but I can't figure out how to incorporate it with the script that you wrote for me. Here it is:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
var now=new Date();
nowM=now.getMonth()+1;
nowD=now.getDate();
nowM=(nowM<10)?'0'+nowM:nowM;
nowD=(nowD<10)?'0'+nowD:nowD;
var adr=nowM+nowD+'fra.htm';
function insertSrc(){
if(document.layers){
document.layers['todayInH_NS'].src=adr;
}
else{
document.getElementById('todayInH').src=adr;
}
}
//-->
</script>
</head>
<body onload="insertSrc()">
<layer id="todayInH_NS"></layer>
<iframe src="" id="todayInH"></iframe>
</script>
</body>
</html>
If you have any thoughts or advice, they would be greatly appreciated!
Thanks,
Amanda
crh3675
07-07-2004, 03:17 PM
Ok, here's a shot:
<script type="text/javascript">
function loadRss(){
var curDate=new Date();
var dateString=curDate.getFullYear()+"-"+(curDate.getMonth()+1)+"-"+curDate.getDate()
var path="data/english-"+dateString+".xml";
var targetFrame=null;
if(document.layers){
targetFrame=document.layers.rssFrame;
}else if(document.getElementById){
targetFrame=document.getElementById("rssFrame");
}
targetFrame.src=path;
}
</script>
<a href="javascript:loadRss()">click</a><br>
<layer style="position:absolute" top="50" left="500" width="400" height="500" name="rssFrame"></layer>
<iframe src="" id="rssFeed" name="rssFrame" frameborder="0" scrolling="no"width="400px" height="500px">
Unfortunately, you cannot access the SRC attribute of an ILAYER. You need to use a regular LAYER that is positioned.
OO7girl
07-08-2004, 07:30 AM
Thanks for your speedy reply!
The one thing I am not clear on is what I do with this part of the code:
<a href="java script:loadRss()">click</a><br>
Will the user have to click on a link to load up the layer for Netscape, and where in the body of my HTML doc should I put the above line?
OO7girl
07-08-2004, 07:38 AM
Oh dear - I seem to have run into another problem.
I'm just testing out everything on my free web server, and I keep getting a runtime error that cites a syntax error with Line 541 of my code, although when I click either "yes" or "no", everything displays properly. There IS no Line 541 - my HTML document is only 540 lines long. What could this mean?
Here is the URL:
http://www25.brinkster.com/oo7girl/vimy/Calendar%20Test/curiosity4.html
OO7girl
07-08-2004, 08:30 AM
Nevermind - figured out the runtime error problem (had to do with the stupid Javascript-generated banners on Brinkster), but my other question about the Netscape code still stands. :)
Thanks!
crh3675
07-08-2004, 11:51 AM
I just added that so you can see the SRC change for the tag. You can move it to the onload of the body:
<body onload="loadRSS()">
OO7girl
07-08-2004, 12:18 PM
I'm not sure I understand what you mean - do I only need to add everything from <script> to </script> to the head of my document, then add onload="loadRSS()" to the body tag, and lastly add that last chunk of code that deals with the actual layer and iframe?
I've done that and I'm still getting a "this page cannot be displayed" message in the iframe when I test the page in IE and a blank space when I test it in Netscape.
OO7girl
07-08-2004, 12:30 PM
I got it to work! Thank you kindly. :D
OO7girl
07-08-2004, 01:08 PM
Geez - no sooner do I get it to work than I realize that NS 4.79 gives me an error message because the script you wrote is essentially blocking the content that an iframe is trying to load. Apparently this is a known problem. Argh.
Would there be an easy way to make the browser (if NS 4.79) load up the correct XML file (I'm now using XML files formatted with an external stylesheet, not HTML files) in a popup window?
crh3675
07-08-2004, 01:17 PM
Can you provide a URL location of this code so I can take a look? What you would need to do is send a content type header of
ascii/text
OO7girl
07-08-2004, 01:43 PM
Sure, I can send you the URL, but I'm afraid I don't follow re: the content type header of ascii/text. Do you mean I should use that in the HTML doc?
http://www25.brinkster.com/oo7girl/vimy/Calendar%20Test/ccode.html