Chapter: Internet & World Wide Web HOW TO PROGRAM - The Ajax Client - XML and RSS

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

RSS

RSS stands for RDF (Resource Description Framework) Site Summary and is also known as Rich Site Summary and Really Simple Syndication.

RSS

 

RSS stands for RDF (Resource Description Framework) Site Summary and is also known as Rich Site Summary and Really Simple Syndication. RSS is an XML format used to syndicate website content, such as news articles, blog entries, product reviews, pod-casts, vodcasts and more for inclusion on other websites. An RSS feed contains an rss root element with a version attribute and a channel child element with item subelements.

 

Depending on the RSS version, the channel and item elements have certain required and optional child elements. The item elements provide the feed subscriber with a link to a web page or file, a title and description of the page or file. The most commonly used RSS feed versions are 0.91, 1.0, and 2.0, with RSS 2.0 being the most popular version. We dis-cuss only RSS version 2.0 in this section.

RSS version 2.0, introduced in 2002, builds upon the RSS 0.9x versions. Version 2.0 does not contain length limitations or item element limitations of earlier versions, makes some formerly required elements optional, and adds new channel and item subelements. Removing length limitations on item descriptions allows RSS feeds to contain entire arti-cles, blog entries and other web content. You can also have partial feeds that provide only a summary of the syndicated content. Partial feeds require the RSS subscriber to visit a website to view the complete content. RSS 2.0 allows item elements to contain an enclo-sure element providing the location of a media file that is related to the item. Such enclo-sures enable syndication of audio and video (such as podcasts and vodcasts) via RSS feeds.

 

By providing up-to-date, linkable content for anyone to use, RSS enables website developers to draw more traffic. It also allows users to get news and information from many sources easily and reduces content development time. RSS simplifies importing information from portals, weblogs and news sites. Any piece of information can be syndi-cated via RSS, not just news. After putting information in RSS format, an RSS program, such as a feed reader or aggregator, can check the feed for changes and react to them. For more details on RSS and for links to many RSS sites, visit our RSS Resource Center at  www.deitel.com/RSS.

 

RSS 2.0 channel and item Elements

In RSS 2.0, the required child elements of channel are description, link and title, and the required child element of an item is either title or description. Figures 14.36– 14.37 overview the child elements of channels and items, respectively.





Browsers and RSS Feeds

 

Many of the latest web browsers can now view RSS feeds, determine whether a website offers feeds, allow you to subscribe to feeds and create feed lists. An RSS aggregator keeps tracks of many RSS feeds and brings together information from the separate feeds. There are many RSS aggregators available, including Bloglines, BottomFeeder, FeedDemon, Mi-crosoft Internet Explorer 7, Mozilla Firefox 2.0, My Yahoo, NewsGator and Opera 9.

 

To allow browsers and search engines to determine whether a web page contains an RSS feed, a link element can be added to the head of a page as follows:

 

<link rel = "alternate" type = "application/rss+xml" title = "RSS" href = "file">

 

Many sites provide RSS feed validators. Some examples of RSS feed validators are validator.w3.org/feed, feedvalidator.org, and  www.validome.org/rss-atom/.

 

Creating a Feed Aggregator

The DOM and XSL can be used to create RSS aggregators. A simple RSS aggregator uses an XSL stylesheet to format RSS feeds as XHTML. Figure 14.38 loads two XML docu-ments—an RSS feed (a small portion of which is shown in Fig. 14.39) and an XSL style sheet—then uses JavaScript to apply an XSL transformation to the RSS content and ren-der it on the page. You’ll notice as we discuss this program that there is little commonality between Internet Explorer 7 and Firefox with regard to programmatically applying XSL transformations. This is one of the reasons that JavaScript libraries have become popular in web development—they tend to hide such browser-specific issues from you. We discuss the Dojo toolkit—one of many popular JavaScript libraries—in Section 15.8. For more information on JavaScript libraries, see our JavaScript and Ajax Resource Centers

( www.deitel.com/JavaScript/ and  www.deitel.com/Ajax/, respectively).

 

Determining the Browser Type and Loading the Documents

When this page first loads, lines 19–23 (Fig. 14.38) determine whether the browser is In-ternet Explorer 7 or Firefox 2 and store the result in variable browser for use throughout the script. After the body of this XHTML document loads, its onload event calls function start (lines 26–48) to load RSS and XSL files as XML documents, and to transform the RSS. Since Internet Explorer 7 can download the files synchronously, lines 30–33 perform the loading, transformation and display steps sequentially. As mentioned previously, Fire-fox 2 loads the files asynchronously. For this reason, line 37 starts loading the rss.xsl document (included with this example’s code), and lines 38–46 register an onload event handler for that document. When the document finishes loading, line 40 begins loading the deitel-20.xml RSS document. Lines 41–45 register an onload event handler for this second document. When it finishes loading, lines 43–44 perform the transformation and display the results.

 

Transforming the RSS to XHTML

Function applyTransform (Fig. 14.38, lines 75–96) performs the browser-specific XSL transformations using the RSS document and XSL document it receives as arguments. Line 81 uses the MSXML object’s built-in XSLT capabilities to apply the transformations.

 

Method transformNode is invoked on the rssDocument object and receives the xslDocument object as an argument.

 

13<?xml version = "1.0" encoding = "utf-8"?>

 

14<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

 

15       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

16          

3    <!-- Fig. 14.38: RssViewer.html -->

 

4    <!-- Simple RSS viewer. -->

 

5    <html xmlns = "http://www.w3.org/1999/xhtml">

 

6    <head>

 

7          <title>Simple RSS Viewer</title>

 

8             <style type = "text/css">

 

9                   #outputDiv { font: 12px Verdana, Geneva, Arial,

12                  Helvetica, sans-serif; }

 

8             </style>

 

9             <script type = "text/javascript">

10         <!--

 

11         var browser = ""; // used to determine which browser is being used

12          

12         // is the browser Internet Explorer 7 or Firefox 2?

 

13         if ( window.ActiveXObject ) // IE7

 

14                browser = "IE7";

 

15         else if ( document.implementation &&

 

16                document.implementation.createDocument ) // FF2 and other browsers

 

17                browser = "FF2";

24

4             // load both the RSS feed and the XSL file to process it

 

5             function start()

6             {

 

7                   if ( browser == "IE7" )

8                   {

 

9                         var xsl = loadXMLDocument( 'rss.xsl' ); // load XSL file

 

10                      var rss = loadXMLDocument( 'deitel-20.xml' ); // load RSS feed

 

11                      var result = applyTransform( rss, xsl ); // apply transform

 

12                      displayTransformedRss( result ); // display feed info

 

13                } // end if

 

14                else if ( browser == "FF2" )

15                {

 

16                      var xsl = loadXMLDocument( 'rss.xsl' ); // load XSL file

 

17                      xsl.onload = function() // function to execute when xsl loads

{

var rss = loadXMLDocument( 'deitel-20.xml' ); // load RSS feed

 

rss.onload = function() // function to execute when rss loads

{          

var result = applyTransform( rss, xsl ); // apply transform

displayTransformedRss( result );  // display feed info

7                                } // end onload event handler for rss

 

8                         } // end onload event handler for xsl

 

9                   } // end else

 

10         } // end function start

49

11         // load XML document based on whether the browser is IE7 or Firefox 2

 

12         function loadXMLDocument( url )

13         {

 

14                var doc = ""; // variable to manage loading file


54

21               if ( browser == "IE7" ) // IE7

22               {

 

23                      // create IE7-specific XML document object

 

24                      doc = new ActiveXObject( "Msxml2.DOMDocument.6.0" );

 

25                      doc.async = false; // specifies synchronous loading of XML doc

 

26                      doc.load( url ); // load the XML document specified by url

 

27               } // end if

 

28               else if ( browser == "FF2" ) // other browsers

29               {

 

30                      // create XML document object

 

31                      doc = document.implementation.createDocument( "", "", null );

 

32                      doc.load( url ); // load the XML document specified by url

 

33               } // end else

 

34               else // not supported

 

35                      alert( 'This script is not supported by your browser' );

70

3                   return doc; // return the loaded document

 

4            } // end function loadXMLDocument

73

1            // apply XSL transformation and show results

 

2            function applyTransform( rssDocument, xslDocument )

3            {

 

4                   var result; // stores transformed RSS

78

5                   // transform the RSS feed to XHTML

 

6                   if ( browser == "IE7" )

 

7                         result = rssDocument.transformNode( xslDocument );

 

8                   else // browser == "FF2"

9                   {

 

10                      // create Firefox object to perform transformation

 

11                      var xsltProcessor = new XSLTProcessor();

86

1                         // specify XSL stylesheet to use in transformation

 

2                         xsltProcessor.importStylesheet( xslDocument );

89

5                         // apply the transformation

6                         result =

14                            xsltProcessor.transformToFragment( rssDocument, document );

 

15               } // end else

94

1                   return result; // return the transformed RSS

 

2            } // end function applyTransform

97

5            // display the XML document and highlight the first child

 

6            function displayTransformedRss( resultXHTML )

11         {

12                if ( browser == "IE7" )

 

13                      document.getElementById( "outputDiv" ).innerHTML = resultXHTML;

 

14                else // browser == "FF2"

 

15                      document.getElementById( "outputDiv" ).appendChild(

 

16                             resultXHTML );

 

17         } // end function displayTransformedRss


 

 

 

15 // -->

 

16          </script>

 

17   </head>

 

18   <body id = "body" onload = "start();">

 

19          <div id = "outputDiv"></div>

 

20   </body>

 

21   </html>

 

 


Fig. 14.38 | Rendering an RSS feed in a web page using XSLT and JavaScript.

 

 

 

1  <?xml version="1.0" encoding="utf-8"?>

 

2

23 <!-- Fig. 14.39: deitel-20.xml -->

 

24<!-- RSS 2.0 feed of Deitel Resource Centers -->

 

25<rss version="2.0">

 

26       <channel>

 

27             <title>

 

28                   Internet &#38; World Wide Web How to Program:

 

29                   Deitel Resource Centers

 

30                </title>


1                   <link>http://www.deitel.com/ResourceCenters.html</link>

 

2                   <description>

 

3                         Check out our growing network of Resource Centers that focus on

 

4                         many of today's hottest programming, Web 2.0 and technology

 

5                         topics. Start your search here for downloads, tutorials,

 

6                         documentation, books, e-books, blogs, RSS feeds, journals,

 

7                         articles, training, webcasts, podcasts, videos and more.

 

8                   </description>

 

9                   <languague>en-us</languague>

 

10                <image>

 

11                      <url>

4                                http://www.deitel.com/Portals/0/deitel_transparent_smaller.png

 

5                         </url>

 

6                         <title>Deitel.com</title>

 

7                         <link>http://www.deitel.com/</link>

 

8                   </image>

27

7                   <item>

 

8                         <title>Adobe® Flex</title>

 

9                         <link>http://www.deitel.com/Flex/</link>

 

<description>

32        <p>    

33        Welcome to the Adobe® Flex™ Resource Center. Adobe Flex 2 is a

34        rich Internet application (RIA) framework that allows you to

35        create scalable, cross-platform, multimedia-rich            applications

36        for delivery within the enterprise or across the   Internet.

37        Start your search here for resources, downloads,         tutorials,

38        documentation, books, e-books, articles, blogs and more that

39        will help you develop Flex applications. 

11 </p>

 

12                      </description>

 

13                      <category>Programming</category>

 

14                </item>

 

15         </channel>

 

16   </rss>

 

Fig. 14.39 | RSS 2.0 sample feed.

 

Firefox provides built-in XSLT processing in the form of the XSLTProcessor object (created at line 85). After creating this object, you use its importStylesheet method to specify the XSL stylesheet you’d like to apply (line 88). Finally, lines 91–92 apply the

 

transformation by invoking the XSLTProcessor object’s transformToFragment method,

 

which returns a document fragment—i.e., a piece of a document. In our case, the rss.xsl document transforms the RSS into an XHTML table element that we’ll append to the outputDiv element in our XHTML page. The arguments to transformToFragment are the document to transform and the document object to which the transformed fragment will belong. To learn more about XSLTProcessor, visit developer.mozilla.org/en/ docs/The_XSLT/JavaScript_Interface_in_Gecko.

 

In each browser’s case, after the transformation, the resulting XHTML markup is assigned to variable result and returned from function applyTransform. Then function

displayTransformedRss is called.

 

Displaying the XHTML Markup

 

Function displayTransformedRss (lines 99–106) displays the transformed RSS in the outputDiv element (line 111 in the body). In both Internet Explorer 7 and Firefox 2, we use the DOM method getElementById to obtain the outputDiv element. In Internet Ex-plorer 7, the node’s innerHTML property is used to add the table as a child of the outputDiv element (line 102). In Firefox, the node’s appendChild method must be used to append the table (a document fragment) to the outputDiv element.

 


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.