HTML Post Loading and Processing Resources using AJAX – Part 4: defining a refresh interval, client side XSLT transformations and RSS processing

0

This is the fourth installment in a series of four on the concept of
Post Loading Resources into an HTML document after the document itself
has been loaded. Using AJAX techniques and simple JavaScript library -
the PLR framework – we can very easily get the browser to get hold of
additional contents, after the document has been loaded. This content
can come from a variety of sources, local or remote (using a Proxy
Servlet), and can be processed in many ways. We have seen in the second
installment how we simply paste the post loaded content into the
innerHTML property of container elements like DIV or TD or the value
property of Form elements. Part 3 discusses a more advanced version of
PLR that can handle custom PLR processors as well as dependencies
between differen PLR objects. This part also discussed the concept of
HTML scraping. It also demonstrates how post loaded resources can be
JavaScript libraries that can be dynamically added to the document. We
have used that functionality to dynamically create SELECT elements and
subsequently dynamically populate these lists with post loaded data.

In
this part we will build upon the previous articles and add
functionality to define a refresh interval for Post Load Resource
objects. The PLR framework will re-load the resource every time the
refresh interval expires. Thus we can include news-headlines sections
in portlet-like frames that are periodically refreshed. This article
also discusses client-side XSLT transformations, used for processing
external RSS-feeds as well as processing XML fragments that are the
result of (X)HTML scraping.

The previous installments are listed here: 

....
 

Client Side XSLT transformation using AJAXSLT (powered by Google)

Since
many of the resources we end up Post Loading are in fact XML documents
- (scraped) XHTML, plain XML, RSS, SOAP – it will be a common
requirement to do XSLT transformations as part of the processing of
these resources. Most modern browsers have built-in support for XSLT
transformations – though the implementations across browsers differ. I
choose to make use of the AJAXSLT library from Google for doing the
client side transformations. I have discussed AJAXSLT in an earlier
article: Introducing AJAXSLT – library for client side, JavaScript, XSLT transformations (good for RIA and AJAX) This article explains where to get AJAXSLT, how to set it up and how to use it.

For
the Post Load Resource library, nothing really changes. We have to
include the AJAXSLT JS-libraries in our HTML document – or load them
themselves as PLR, which is an option:

    &lt;script src=&quot;misc.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;<br />    &lt;script src=&quot;dom.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;<br />    &lt;script src=&quot;xpath.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;<br />    &lt;script src=&quot;xslt.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;<br /><br />

The
XSLT used for the transformation is a PLR and the XML content another
PLR, that depends on the XSLT. This is very easily specified through
the PLR framework:

    &lt;h3&gt;Show the contents of a local XML document - XSLT transformed in the client&lt;/h3&gt;<br />    &lt;DIV style=&quot;background-color :yellow;height:150px&quot;&gt;<br />      &lt;pre id=&quot;PL1&quot;&gt;<br />        &lt;Script language=&quot;JavaScript&quot;&gt;    <br />          var deptXsltPLR = addPostLoadResource(null, 'dept2html.xsl', false, 'XSLT_FOR_DEPT_LOADER');<br />          addPostLoadResource('PL1', 'dept.xml', false, 'POSTLOAD_DEPARTMENTS', customXMLXSLTrocessPostLoad, new Array(deptXsltPLR));<br />        &lt;/Script&gt;<br />      &lt;/pre&gt;<br />    &lt;/DIV&gt;<br />&nbsp;

Here
we set up a DIV and we specify the two PLR objects: first the
deptXsltPLR which post loads the dept2html.xsl stylesheet. This PLR is
not linked to a custom processor or a DOM element – which is logical as
we will not process this XSLT document itself. Next we set up the PLR
for the dept.xml resource; this PLR depends on the deptXsltPLR and it
uses the customXMLXSLTProcessPostLoad function for processing the XML
PLR.

The XML document in this case looks like:

&nbsp;&lt;?xml version=&quot;1.0&quot;?&gt;<br />&lt;DEPT&gt;<br /> &lt;ROW&gt;<br />  &lt;DEPTNO&gt;10&lt;/DEPTNO&gt;<br />  &lt;DNAME&gt;ACCOUNTING&lt;/DNAME&gt;<br />  &lt;LOC&gt;NEW YORK&lt;/LOC&gt;<br /> &lt;/ROW&gt;<br /> &lt;ROW&gt;<br />  &lt;DEPTNO&gt;20&lt;/DEPTNO&gt;<br />  &lt;DNAME&gt;RESEARCH&lt;/DNAME&gt;<br />  &lt;LOC&gt;DALLAS&lt;/LOC&gt;<br /> &lt;/ROW&gt;<br /> &lt;ROW&gt;<br />  &lt;DEPTNO&gt;30&lt;/DEPTNO&gt;<br />  &lt;DNAME&gt;SALES&lt;/DNAME&gt;<br />  &lt;LOC&gt;CHICAGO&lt;/LOC&gt;<br /> &lt;/ROW&gt;<br /> &lt;ROW&gt;<br />  &lt;DEPTNO&gt;40&lt;/DEPTNO&gt;<br />  &lt;DNAME&gt;OPERATIONS&lt;/DNAME&gt;<br />  &lt;LOC&gt;BOSTON&lt;/LOC&gt;<br /> &lt;/ROW&gt;<br />&lt;/DEPT&gt;<br /><br />

And the XSLT stylesheet we use for transforming it is this one:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;windows-1252&quot; ?&gt;<br />&lt;xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;<br />  &lt;!-- Root template --&gt;<br />  &lt;xsl:template match=&quot;/&quot;&gt;<br />    &lt;H2&gt;Departments&lt;/H2&gt;<br />      &lt;ul&gt;<br />        &lt;xsl:for-each select=&quot;DEPT/ROW&quot;&gt;<br />         &lt;li&gt;<br />           &lt;xsl:value-of select=&quot;DNAME&quot; /&gt;<br />           &lt;xsl:text&gt; - located in &lt;/xsl:text&gt;<br />           &lt;xsl:value-of select=&quot;LOC&quot; /&gt; <br />         &lt;/li&gt;<br />        &lt;/xsl:for-each&gt;<br />      &lt;/ul&gt;<br />  &lt;/xsl:template&gt;<br />&lt;/xsl:stylesheet&gt;<br />&nbsp;

The function that will do the processing: 

 

  function customXMLXSLTrocessPostLoad() {<br />    var postLoadResource = this;<br />    var xml = postLoadResource.req.responseXML;<br />    var xslt = xmlParse(postLoadResource.dependsOn[0].req.responseText);     <br />    var html = xsltProcess(xml, xslt);<br />    el(this.elementId).innerHTML = html;    <br />  }<br />&nbsp;

This
function implements a more or less generic way of processing an
XML/XSLT couple of Post Load Resource objects. It assumes that the
context object is the XML PLR and that the first of its dependencies
-dependsOn[0]- refers to the XSLT stylesheet. It then uses the AJAXSLT
library for parsing the XSLT document and performing the
transformation. The result is pasted as an HTML fragment into the
innerHTML property of the target DOM element. The result is shown below:

Combining HTML Scraping with XSLT Transformation

If
we use HTLM scraping – use a web-page as external resource and extract
a fragment from it – we could use XSLT for the post-load processing: if
the HTML fragment is in fact an XHTML or valid XML document, we can
apply the same type of processing used above. In our next example, we
will take the AMIS Technology Weblog as our external source. This fine
blog with lots of interesting articles on fantastic subjects like AJAX,
EJB 3.0, SOA and BPEL, and so much more, provides an RSS feed as well,
but for some reason we want to scrape the homepage:

What
we are interested in, is the list of recent posts. This is found in the
HTML page on the left hand side, probably inside a DIV and presented as
an unordered list. Close inspection of the page source gives us the
clues needed to scrape that recents posts content:

  function customProcessAMISBLOG() {<br />    // get hold of new posts - that is the UL element following the string Recent Posts <br />    var postLoadResource = this;<br />    var pos = postLoadResource.req.responseText.indexOf('Recent Posts'); <br />    pos = postLoadResource.req.responseText.indexOf('&lt;ul&gt;', pos);<br />    // now the end of the &lt;ul&gt; demarcates the end of the list of Recents Posts<br />    var endPos = postLoadResource.req.responseText.indexOf('&lt;/ul&gt;', pos);        <br />    // store in string, parse as xml<br />    var xmlString = &quot;&lt;recentPosts&gt;&quot;+postLoadResource.req.responseText.substring(pos, endPos+5)+&quot;&lt;/recentPosts&gt;&quot;;<br />    // transform<br />    var xml = xmlParse(xmlString);<br />    var xslt = xmlParse(postLoadResource.dependsOn[0].req.responseText); <br />    var html = xsltProcess(xml, xslt);<br />    el(postLoadResource.elementId).innerHTML=html;<br />  }<br />&nbsp;

Inside the HTML document returned from http://technology.amis.nl/blog/index.php
we find the string Recent Posts. We locate the first <UL> element
after that, as well as the corresponding </ul> element. The
entire fragment between these two tags is wrapped in a
<recentPosts> element and parsed as XML document. Subsequently,
it is transformed used the following XSLT:

&lt;?xml version='1.0' encoding='windows-1252'?&gt;<br />&lt;xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;<br />  &lt;xsl:template match=&quot;/&quot;&gt;<br />    &lt;div style=&quot;color:green&quot;&gt;<br />      &lt;xsl:value-of select=&quot;'AMIS Technology Weblog - Recent Posts'&quot;/&gt;<br />      &lt;ul&gt;<br />        &lt;xsl:for-each select=&quot;recentPosts/ul/li&quot;&gt;<br />          &lt;li&gt;<br />          &lt;xsl:copy-of select=&quot;a&quot; /&gt;<br />          &lt;/li&gt;<br />        &lt;/xsl:for-each&gt;<br />      &lt;/ul&gt;<br />    &lt;/div&gt;<br />  &lt;/xsl:template&gt;<br />&lt;/xsl:stylesheet&gt; <br />

The result of this transformation looks like:

 

The snippet used in the HTML document to set up this AMIS Blog scraper is the following:

    &lt;h3&gt;Download the frontpage of the AMIS Technology Weblog, scrape the list of recent posts and do an XSLT transformation&lt;/h3&gt;<br />    &lt;DIV class=&quot;left&quot; id=&quot;PL7&quot; &gt;<br />    &lt;/DIV&gt;<br />      &lt;Script language=&quot;JavaScript&quot;&gt;    <br />      var xsltblogPLR = addPostLoadResource(null, 'amisblog2html.xsl', false, 'XSLT_FOR_AMISBLOG_LOADER');<br />      addPostLoadResource('PL7', 'technology.amis.nl/blog/index.php', true, 'POSTLOAD_AMISBLOG_LOADER', customProcessAMISBLOG,new Array(xsltblogPLR));<br />      &lt;/Script&gt;<br /><br />

Post Loading and Processing RSS Feeds

A
far more organized approach than HTML Scraping to
headlines-from-other-websites is the use of RSS feeds. RSS feeds are
simple XML based documents that can very easily be transformed. In this
next example, we will Post Load the RSS feed for the BBC News Headlines
- this feed is found at http://news.bbc.co.uk/rss/newsonline_world_edition/front_page/rss091.xml
- using a simple XSLT stylesheet. We can use the generic function
customXMLXSLTrocessPostLoad() to handle the XML and XSLT resources. The
specifiction of this RSS Reader in the HTML document is the following
snippet:

    &lt;h3&gt;Postload the BBC News RSS feed, an XSLT stylesheet and transform to HTML (refresh the newsfeed every 2 minutes) &lt;/h3&gt;<br />      &lt;Script language=&quot;JavaScript&quot;&gt;    <br />        var xsltPLR = addPostLoadResource('xml', 'rss2html.xsl', false, 'XSLT_FOR_RSS_LOADER');<br />        addPostLoadResource('PL4', 'news.bbc.co.uk/rss/newsonline_world_edition/front_page/rss091.xml', true, 'POSTLOAD_BBCRSS_LOADER', customXMLXSLTrocessPostLoad, new Array(xsltPLR), 120);<br />      &lt;/Script&gt;<br />    &lt;DIV class=&quot;right&quot; id=&quot;PL4&quot; style=&quot;width:60%&quot;&gt;<br />    &lt;/DIV&gt;<br />&nbsp;

The
XSLT stylesheet used for transforming the RSS feed from Auntie Beeb to
pretty HTML (well, you can read it, can’t you?) is the following:

<?xml version=’1.0′ encoding=’windows-1252′?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <div style="color:green">
      <h3><xsl:value-of select="rss/channel/title/."/></h3>
    <ul>
      <xsl:for-each select="rss/channel/item">
        <li>
          <xsl:element name="a">
            <xsl:attribute name="target">
              <xsl:text>_blank</xsl:text>
            </xsl:attribute>
            <xsl:attribute name="href">
              <xsl:value-of select="link"/>
            </xsl:attribute>
            <xsl:value-of select="title"/>
          </xsl:element>
        </li>
      </xsl:for-each>
    </ul>
    </div>
  </xsl:template>
</xsl:stylesheet>
 

The feed itself looks like this:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot; ?&gt;<br />&lt;?xml-stylesheet title=&quot;XSL_formatting&quot; type=&quot;text/xsl&quot; href=&quot;/shared/bsp/xsl/rss/nolsol.xsl&quot;?&gt;<br />&lt;rss version=&quot;2.0&quot;&gt;<br /> &lt;channel&gt;<br />  &lt;title&gt;BBC News | News Front Page | World Edition&lt;/title&gt;<br />  &lt;link&gt;http://news.bbc.co.uk/go/rss/-/2/hi/default.stm&lt;/link&gt;<br />...<br />  &lt;copyright&gt;Copyright: (C) British Broadcasting Corporation, see<br />             http://news.bbc.co.uk/2/hi/help/rss/4498287.stm for terms and<br />             conditions of reuse&lt;/copyright&gt;<br />  &lt;docs&gt;http://www.bbc.co.uk/syndication/&lt;/docs&gt;<br />  &lt;ttl&gt;15&lt;/ttl&gt;<br />  &lt;image&gt;<br />   &lt;title&gt;BBC News&lt;/title&gt;<br />   &lt;url&gt;http://news.bbc.co.uk/nol/shared/img/bbc_news_120x60.gif&lt;/url&gt;<br />   &lt;link&gt;http://news.bbc.co.uk&lt;/link&gt;<br />  &lt;/image&gt;<br />  &lt;item&gt;<br />   &lt;title&gt;Bush vows to face dangers head on&lt;/title&gt;<br />   &lt;description&gt;The US president warns of &quot;danger and decline&quot; if the US fails<br />                to face down threats, in his State of the Union speech.&lt;/description&gt;<br />   &lt;link&gt;http://news.bbc.co.uk/go/rss/-/2/hi/americas/4665758.stm&lt;/link&gt;<br />   &lt;guid isPermaLink=&quot;false&quot;&gt;http://news.bbc.co.uk/2/hi/americas/4665758.stm&lt;/guid&gt;<br />   &lt;pubDate&gt;Wed, 01 Feb 2006 03:54:34 GMT&lt;/pubDate&gt;<br />   &lt;category&gt;Americas&lt;/category&gt;<br />  &lt;/item&gt;<br />  &lt;item&gt;<br />   &lt;title&gt;Nepal elections 'will go ahead'&lt;/title&gt;<br />   &lt;description&gt;King Gyanendra of Nepal says local elections will go ahead next<br />                week, speaking a year after he took direct power.&lt;/description&gt;<br />   &lt;link&gt;http://news.bbc.co.uk/go/rss/-/2/hi/south_asia/4668646.stm&lt;/link&gt;<br />   &lt;guid isPermaLink=&quot;false&quot;&gt;http://news.bbc.co.uk/2/hi/south_asia/4668646.stm&lt;/guid&gt;<br />   &lt;pubDate&gt;Wed, 01 Feb 2006 04:40:03 GMT&lt;/pubDate&gt;<br />   &lt;category&gt;South Asia&lt;/category&gt;<br />  &lt;/item&gt;<br /> ....<br />

You
can see it would be easy to extend the RSS portlet a little with a
summary of the news or an indication of the age of the item. 

The Refresh Interval

It
is quite simple to add a Refresh Interval to the PLR framework. We add
it as a new property to the PLR object. Of course, we have to support
that new property in the functions called to add new Post Load Resource
objects:

/*--- content loader object for cross-browser requests ---*/<br />amis.PostLoadResource=function           <br />                   ( elementId<br />                   , url<br />                   , requireProxy // boolean indicating whether or not the resource must be acquired through a proxy from a remote domain<br />                   , label<br />                   , processor // function reference of function that will process the resource when received<br />                   , dependsOn // other PostLoadResources this PLR may depend on, such as an XSLT that an XML depends on for being processed<br />                   , refreshTime // time interval in seconds after which the resource should be reloaded and reprocessed<br />                   ) {<br />  ...<br />  this.processor= processor;<br />  this.refreshTime = refreshTime;<br />  this.dependsOn = dependsOn;<br />  ...<br />}<br /><br />  // create a new PLR object and add it to the array of PLR objects to be dealt with when the page has loaded<br />  function addPostLoadResource<br />           ( elementId<br />           , url<br />           , requireProxy // boolean indicating whether or not the resource must be acquired through a proxy from a remote domain<br />           , label<br />           , processor // function reference of function that will process the resource when received<br />           , dependsOn<br />           , refreshTime // in seconds<br />           ) {<br />     var plr = new amis.PostLoadResource(elementId, url, requireProxy,label, processor, dependsOn, refreshTime);<br />     var size = postLoadResources.push( plr ); // add a new PostLoadResource object to the array<br />     plr.id = size -1; // ensure the plr objects knows where it sits in the postLoadResources array<br />     return plr;<br />  }<br />&nbsp;

So
now we have added a refreshTime property. We have to make use of that
property when we have first processed the resource. If at that time we
find that a refresh time was specified, we have to schedule the next
load & execution of the PLR object. That is done in the
startProcessing() function:

  function startProcessing(postLoadResource) {<br />    // check if the postLoadResource depends on other plrs that are not yet processed<br />    // if so, it goes into the waiting room - state= amis.STATE_LOADED_AND_WAITING<br />    ...<br />    postLoadResource.state = amis.STATE_PROCESSED;<br />    if (postLoadResource.refreshTime) {<br />      setTimeout(&quot;handleTimeOut('&quot;+postLoadResource.id+&quot;')&quot;, postLoadResource.refreshTime*1000);<br />    }<br />  }// startProcessing<br />&nbsp;

After
processing is done, we look at the refreshTime property. If it is set,
we call the built-in JavaScript function setTimeout. This function has
two input parameters: a String specifying a function to be called and
the time after which that function should be called. We always call the
same function – handleTimeOut(). Since we cannot pass an object in a
string, we pass the id of the PLR object. This id property was set in
the addPostLoadResource() function and corresponds with the index of
the PLR object in the postLoadResources array. Since the refreshTime
property on the PLR is set in seconds and the setTimeout function takes
the timeout interval in miliseconds, we multiply refreshTime by 1000.

  function handleTimeOut(id) {<br />    postLoadSingleResource(postLoadResources[id]);<br />  }<br />&nbsp;

The
function handleTimeOut is a generic function that deals with PLRs that
need refreshing. The function gets passed in an id parameter, that
refers to a PLR object in the postLoadResources array. This object is
retrieved from the array using the id value and subsequently passed to
postLoadSingleResource. Processing takes place in the same way as when
the document was first loaded.

Note: the mechanism currently does
not refresh the PLR objects that have a dependency on the PLR that is
refreshed. If for example we specify a refresh rate for the XLST
stylesheet that we use to transform the BBC News Headlines RSS, then we
probably should refresh the dependent Headlines PLR itself as well,
since otherwise we see no effect of the refreshed XSLT whatsoever.
Clearly this is easy to add to the framework.

To be complete in this matter, the call that creates the PLR with auto-refresh from the HTML document looks something like:

        addPostLoadResource('PL4', 'news.bbc.co.uk/rss/newsonline_world_edition/front_page/rss091.xml', true, 'POSTLOAD_BBCRSS_LOADER', customXMLXSLTrocessPostLoad, new Array(xsltPLR), 120);<br /><br />

It
is the parameter 120 that indicates that this particular PLR should be
refreshed every 120 seconds. We will now have the BCC News Headlines
refreshed every 2 minutes – which turns out to be far more frequent
than the BBC itself refreshed the feed…

 

Resources

Download the Sources for this article: PostLoadResourceDemo3.zip.

The previous installments in this series are listed here: 

Related articles

Introducing AJAXSLT – library for client side, JavaScript, XSLT transformations (good for RIA and AJAX)

Proxy Servlet for AJAX requests to multiple, remote servers

 

 

Share.

About Author

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director for Fusion Middleware. Consultant, trainer and instructor on diverse areas including Oracle Database (SQL & PLSQL), Service Oriented Architecture, BPM, ADF, Java in various shapes and forms and many other things. Author of the Oracle Press book: Oracle SOA Suite 11g Handbook. Frequent presenter on conferences such as JavaOne, Oracle OpenWorld, ODTUG Kaleidoscope, Devoxx and OBUG. Presenter for Oracle University Celebrity specials.

Comments are closed.