Jump to content

RSS feeds


brogar3
 Share

Recommended Posts

I am trying to incorporate XML/XSLT into an RSS feed so it dynamically updates with changes in information. The source of the info is an XML file on the same server, I just need to figure out how to reference this information without destroying the well-formed RSS code. All the searches I have performed online cover using XSL to translate an RSS feed onto a webpage, not the approach I need. Any thoughts and guidance would be helpful.

Link to comment
Share on other sites

I am not quite understanding you. You want to put XSL into your RSS(XML) file?They are seperate you can't mix the two. The XML/RSS is like a storage for your information...you make it into readable html by using an XSL stylesheet.If I am misunderstanding please correct me.

Link to comment
Share on other sites

Well, you may use PHP or other server side scripting language to translate the data. Here's a sample PHP parser I found on a site:

class RSS_Parser{// {{{ part-variables (all private)var $Counter; // counts each resource of informationvar $Current; // contains the name of the current elementvar $Parent; // contains the name of the parent of the current elementvar $Parser; // the XML parservar $Result; // the array containg all neccesary information// }}} // {{{ constructor/**Prepares the object*@access public*/function RSS_Parser(){$this->Counter = 0;$this->Current = '';$this->Parent = '';if(!($this->Parser = xml_parser_create()))die('Could not create XML parser!');xml_set_element_handler($this->Parser, 'start', 'end');xml_set_character_data_handler($this->Parser, 'data');}// }}} // {{{ start()/**Controls the opening tags*@access private@param resource $Parser XML parser@param string $Element name of the element@param array $Att array containg the attributes of the element*/function start($Parser, $Element, $Att){if(ereg(':', $Element))list($prefix, $this->Current) = explode(':', $Element);else$this->Current = $Element;if(($this->Current == 'CHANNEL') || ($this->Current == 'IMAGE') || ($this->Current == 'ITEM'))$this->Parent = $this->Current;if($this->Current == 'ITEM')$this->Counter++;}// }}} // {{{ end()/**Controls the closing tags*@access private@param resource $Parser XML parser@param string $Element name of the element*/function end($Parser, $Element){$this->Current = '';}// }}} // {{{ data()/**Controls the data*@access private@param resource $Parser XML parser@param string $Data simbolic data of the element*/function data($Parser, $Data){if(($this->Current == '') || (trim($Data) == ''))return;if($this->Parent == 'ITEM')$this->Result['items'][$this->Counter][strtolower($this->Current)] .= stripslashes(htmlspecialchars($Data, ENT_NOQUOTES));if(($this->Parent == 'CHANNEL') || ($this->Parent == 'IMAGE'))$this->Result[strtolower($this->Parent)][strtolower($this->Current)] .= stripslashes(htmlspecialchars($Data, ENT_NOQUOTES));}// }}} // {{{ parse()/**Parses the current document*@access public@param string $file name of the file or URL@return array Array containg the neccesary info*/function parse($file){$this->Result = array();$fp = @fopen($file, 'r');xml_set_object($this->Parser, &$this);while($Data = fread($fp, 4096)){if(!xml_parse($this->Parser, $Data, feof($fp)))break;}xml_parser_free($this->Parser);$this->RSS_Parser();return $this->Result;}// }}}}

The page wich would include the feed must include this:

<?php// Include the parserinclude('parser.php');$p = new RSS_Parser();// The array containg the information from the feed$channel = array();$channel = $p->parse(news.rss');// Visualize the name of the site...echo '<b><a href="'.$channel['channel']['link'].'" ';echo 'target="_blank" ';echo 'title="'.$channel['channel']['description'].'">';echo $channel['channel']['title'].'</a></b><br />';// ... as well as the individual names of every newsforeach($channel['items'] as $item){echo ' » <a href="'.$item['link'].'" target="_blank" title="'.$item['description'].'">'.$item['title'].'</a><br />';}?>

Notice that this code is made from a Bulgarian and that I have (atleast tryed to) translate all the comments inside. I have not tested the code, so I don't know if it will work at all.Also notice that you must change "news.rss" to the actual name of your feed.

Link to comment
Share on other sites

much easier useing cold fusion::)

<!--- get feed from site ---><cfhttp url="http://www12.porsche.com/usa/rss/default.ashx" method="GET" timeout="15"><cfhttpparam type="Header" name="Accept-Encoding" value="deflate;q=0"><cfhttpparam type="Header" name="TE" value="deflate;q=0"></cfhttp><!--- parse xml information ---><cfset objRSS = xmlParse(cfhttp.filecontent)><!--- find length of arrary to look through ---><cfset ArrayLength = arraylen(objRSS.rss.channel.item)><cfset ItemsDisplayed = 0><!--- loop through array to output feed ---><cfloop index="x" from="1" to="#ArrayLength#"><cfoutput><cfset ItemsDisplayed = ItemsDisplayed + 1><a href="#replace(objRSS.rss.channel.item[x].link.xmltext,'&','&','all')#" title="#urlencodedformat(objRSS.rss.channel.item[x].title.xmltext)#" target="_blank"><strong>#objRSS.rss.channel.item[x].title.xmltext#</strong></a><a href="#replace(objRSS.rss.channel.item[x].link.xmltext,'&','&','all')#" title="#urlencodedformat(objRSS.rss.channel.item[x].title.xmltext)#" target="_blank"><img src="#webroot#_images/btn_newwindow.gif" alt="[open article in new window]" width="19" height="15" hspace="5" vspace="0" border="0" /></a><br /><em class="smallfont">(#dateformat(objRSS.rss.channel.item[x].pubdate.xmltext,"mmmm dd, yyyy")#)</em><br /><br /></cfoutput></cfloop><!--- loop through array to output source ---><hr /><blockquote style="text-align:center;"><cfset SourceLength = arraylen(objRSS.rss.channel)><cfloop index="x" from="1" to="#SourceLength#"><cfoutput>© #dateformat(now(),"yyyy")# Porsche Cars North America<br /><a href="#replace(objRSS.rss.channel[x].link.xmltext,'&','&','all')#" title="[goto feed source in new window]" target="_blank">RSS Feed Source</a><br /><em class="smallfont">Page Generated: (#dateformat(now(),"mmmm dd, yyyy")# at #timeformat(now(),"hh:mm:ss tt")#)</em><br /><br /></cfoutput></cfloop></blockquote>

(assuming v2.0 feed):):(:D

Link to comment
Share on other sites

even easier in ASP.Net

 Sub Page_Load(sender As Object, e As EventArgs)   Try    ' Using a live RSS feed... could also use a cached XML file.    Dim strXmlSrc  As String = "YourFeedName"    ' Path to our XSL file.  Changing the XSL file changes the    ' look of the HTML output.  Try toggling the commenting on the    ' following two lines to give it a try.    Dim strXslFile As String = Server.MapPath("YourXSLStyleSheet")    ' Load our XML file into the XmlDocument object.    Dim myXmlDoc As XmlDocument = New XmlDocument()    myXmlDoc.Load(strXmlSrc)    ' Load our XSL file into the XslTransform object.    Dim myXslDoc As XslTransform = New XslTransform()    myXslDoc.Load(strXslFile)    ' Create a StringBuilder and then point a StringWriter at it.    ' We'll use this to hold the HTML output by the Transform method.    Dim myStringBuilder As StringBuilder = New StringBuilder()    Dim myStringWriter  As StringWriter  = New StringWriter(myStringBuilder)    ' Call the Transform method of the XslTransform object passing it    ' our input via the XmlDocument and getting output via the StringWriter.    myXslDoc.Transform(myXmlDoc, Nothing, myStringWriter)    ' Since I've got the page set to cache, I tag on a little    ' footer indicating when the page was actually built.    myStringBuilder.Append(vbCrLf & "<p><em>Cached at: " _        & Now() & "</em></p>" & vbCrLf)    ' Take our resulting HTML and display it via an ASP.NET    ' literal control.    myLabel.Text = myStringBuilder.ToString      Catch	myLabel.Text = "<i>An error has occured in the RSS feeds.</i>"   End Try  End Sub

Link to comment
Share on other sites

I think I need to clarify my question a bit more with some background. Currently we use a WYSIWYG Content Management Tool to update an XML file with weather information. This information is referenced using ASP on our site allowing us to only update one location and call that information to different pages. Once this is accomplished, I go into our RSS file and copy and paste the weather information from the site into the file, save and upload it. I was thinking there ought to be a tool that allows the RSS file to pull from the same XML file as the online weather pages so I don't have to update the RSS independently. It sounds like there is a conflict between programming languages that would allow this to occur. Any thoughts?

Link to comment
Share on other sites

you could write a script in ASP that when you hit submit to update the XML file it copies thecontents of the newly saved XML into the RSS file.Or you could have an ASP page that produces the RSS as output by reading hte contents of the XML file. I have seen this approach lots of times. It would be as simple as connecting to the xml file via ASP (no HTML on page only script <% %>), reading the contents of the XML file then doing a response.write fo the contents.

Link to comment
Share on other sites

even easier in ASP.Net
lol -oh yeah, but my code is faster than yours!!!:) :)
I think I need to clarify my question a bit more with some background. Currently we use a WYSIWYG Content Management Tool to update an XML file with weather information. This information is referenced using ASP on our site allowing us to only update one location and call that information to different pages. Once this is accomplished, I go into our RSS file and copy and paste the weather information from the site into the file, save and upload it. I was thinking there ought to be a tool that allows the RSS file to pull from the same XML file as the online weather pages so I don't have to update the RSS independently. It sounds like there is a conflict between programming languages that would allow this to occur. Any thoughts?

well, aspnetguy has already offered both possible solutions. I feel the later is best. In Cold Fusion, I can create a Cold Fusion page that when loaded queries the database (the same way the other page does) but then instead of outputting it as HTML, I output it as XML and pass it in that format. So, the code that reads the RSS feed gets XMl even though the page is not an XML file. Sounds weid, but that's what he's talking about. This way, you only update the information once, and it is published on the fly.example - I love RSS, tons of stuff to play with:
<cfsetting enablecfoutputonly="yes" showdebugoutput="no"><cfparam name="url.newsID" default="0"><!--- get XML content ---><cfquery name="spNewsUpdates" datasource="#data_source#">	EXEC aap_news_updates	<cfif #url.newsID# GT 0>	@newsID=#url.newsID#	</cfif></cfquery><!--- create XML content ---><cfsavecontent variable="newsXML"><?xml version="1.0" encoding="ISO-8859-1"?><cfoutput><rss version="2.0"><channel><title>Pedialink</title><link>http://www.pedialink.org</link><description>Pedialink News & Updates</description><language>en-us</language><copyright>Copyright #dateformat(now(),"yyyy")#</copyright><lastBuildDate>#dateformat(now(),"ddd, dd mmm yyyy")# #timeformat(now(),"HH:mm:ss")# CST</lastBuildDate><webMaster>csc@aap.org</webMaster>	<cfloop query="spNewsUpdates">	<item>	<author>csc@aap.org</author>	<category>#XMLformat(newsCategory)#</category>	<title>#XMLformat(newsTitle)#</title>	<link>#XMLformat(newsLink)#</link>	<description>#XMLformat(newsText)#</description>	<pubDate>#dateformat(newsActivateStart,"ddd, dd mmm yyyy")# #timeformat(now(),"HH:mm:ss")# CST</pubDate>	</item>	</cfloop></channel></rss></cfoutput></cfsavecontent><cfcontent type="text/xml"><cfoutput>#newsXML#</cfoutput>

the last line makes the XML and makes the file mimitpye XMl so a reader reads it properly.

Link to comment
Share on other sites

lol, maybe on the first render but after that it is cached to the server and loads instantly for every other user.How about a benchmark test...hmmm? :D:)

I was just being sarcastic . . . but since you mention it, a test would be intersting to conduct. Cold Fusion is similar in that the CFM page is compiled into a java cached file . . . . send me a PM on your idea on how this can be tested - this will be interesting. :(Now we just need a competant PHP developer to join us . . . :)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...