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

2

While working on Rich HTML clients that could retrieve information from various sources on different web-servers using multiple AJAX requests, I came across the AJAXSLT project from Google Code. AJAXSLT is a JavaScript library that you can easily import to any HTML document. It allows you to create or retrieve an XML document – for example using an AJAX style XmlHttpRequest -, an XSLT document in similar fashion and perform the XSLT Transformation in the Client. The resulting XML document can be XHTML that is rendered in the browser (DHTML) or can be used to make additional requests or can be fed into an SVG object for graphical display. Or whatever else you would like to do with such an XML result.

I was sort of intrigued by the concept of AJAXSLT, so I downloaded the code from SourceForce – all of its 38Kb. In this article I will describe a simple application that I created using AJAXSLT: an HTML document that presents multiple links to RSS Feeds to the user. The user can select an RSS Feed, press a submit button and see a very readable summary of the RSS Feed’s contents. This is achieved by a Client Side XSLT transformation of the RSS Feed into XHTML that is rendered in situ. The XSLT document is visible to the user and he or she can make changes to the XSLT before re-rendering the RSS feed.

 ....

Steps

To get to the application shown above is really quite simple. The most complex part of this probably is realizing that retrieving RSS feeds from an HTML document is not trivial: the RSS feeds are from different domains, different web server than the original HTML document. AJAX Requests must be to the same domain for security reasons. Hence we cannot simply make AJAX Requests directly to those RSS Feeds. Instead, we make use of a AjaxProxyServlet, a simple servlet that makes the request on behalf of the HTML client. This proxy is introduced in the article Proxy Servlet for AJAX requests to multiple, remote servers.

1. The first step is downloading the AJAXSLT libraries, currently the 0.4 release. I have downloaded the ZIP file (38Kb) and extracted the contents.

 

The test directory contains two HTML files with associated JavaScript libraries that can be opened in a browser to immediately demonstrate the XSLT transformation as well as the XML Parsing and XPath retrieval functionality. Downloading, extracting and first trial run takes less than 10 minutes (probably less than five).

2. I have taken the xslt.html document from the AJAXSLT test directory as my starting point. I have added the AJAX request functions for calling the AjaxProxyServlet – these were introduced in the article on this proxy: Proxy Servlet for AJAX requests to multiple, remote servers. Then I have added the two hyperlinks for the two RSS Feeds – in this case the BBC World News RSS and the OraBlogs Aggregator RSS. These hyperlinks when clicked make a call to the loadXMLDoc function, passing the URL of the feed. This function makes the Ajax Request to the Proxy Servlet that is to retrieve the XML RSS contents and return it to the browser. The function processReqChange will process the response with the RSS Feed contents. It is pasted into the xml TEXTAREA.

When the Process button is clicked, the function test_xslt() is invoked. This function takes the XML textarea and parses the value into a variable. It then parses the XSLT textarea into another variable. Next it transforms the xsml variable using the xslt variable, storing the result in the html variable. The html variable is subsequently put in the html textarea and in the innerHTML propertyof the DIV htmldisplay:

    function test_xslt() {<br />      var xml = xmlParse(el(&amp;apos;xml&amp;apos;).value);<br />      var xslt = xmlParse(el(&amp;apos;xslt&amp;apos;).value);<br />      var html = xsltProcess(xml, xslt);<br />      el(&amp;apos;html&amp;apos;).value = html;<br />      el(&amp;apos;htmldisplay&amp;apos;).innerHTML = html;<br />    }<br /><br />

The XSLT engine in AJAXSLT is not complete yet. A few features that are required by the XSLT and XPath standards were left out. They are marked in the source code using alert() statements. The initial XSLT document used in this demo application is not extremely complex but at the same time it uses a number of key XSLT features:

&lt;xsl:stylesheet&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;rss/channel/title/.&quot;/&gt;<br />     &lt;/div&gt;<br />     &lt;ul&gt;<br />     &lt;xsl:for-each select=&quot;rss/channel/item&quot;&gt;<br />       &lt;li&gt;<br />         &lt;xsl:element name=&quot;a&quot;&gt;<br />           &lt;xsl:attribute name=&quot;target&quot;&gt;&lt;xsl:text&gt;_blank&lt;/xsl:text&gt;&lt;/xsl:attribute&gt;<br />           &lt;xsl:attribute name=&quot;href&quot;&gt;&lt;xsl:value-of select=&quot;link&quot; /&gt;&lt;/xsl:attribute&gt;<br />           &lt;xsl:value-of select=&quot;title&quot; /&gt;<br />         &lt;/xsl:element&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 />

Note that:AJAXSLT is an implementation of XSL-T in JavaScript, intended for use in fat web pages, which are nowadays referred to as AJAX applications. Because XSL-T uses XPath, it is also an implementation of XPath that can be used independently of XSL-T. Both XPath and XSL-T are built into some of the modern browsers,  however not in all. This library runs on many browsers, including those that don’t support XSLT adaequately or at all, which is why the  library was written in the first place. Supported browsers:Firefox/1.0, Internet Explorer/6.0, Safari/1.2, Safari/1.3, Safari/2.0, Opera/7.5, Opera/8.0.

The entire HTML document looks as follows: 

&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;<br />&lt;html&gt;<br />  &lt;head&gt;<br />    &lt;title&gt;Simple XSLT test&lt;/title&gt;<br />    &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 />    &lt;script src=&quot;test/xslt_script.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;<br /><br />&lt;SCRIPT language=&quot;JavaScript&quot;&gt;    <br /><br />logging = true;<br />xsltdebug = true;<br />    var req;<br />    var xmldoc;<br /><br />function el(id) {<br />  return document.getElementById(id);<br />}<br /><br />function test_xslt() {<br />  var xml = xmlParse(el('xml').value);<br />  var xslt = xmlParse(el('xslt').value);<br />  var html = xsltProcess(xml, xslt);<br />  el('html').value = html;<br />  el('htmldisplay').innerHTML = html;<br />}<br /><br />function cleanxml() {<br />  cleanvalue('xml');<br />  cleanvalue('xslt');<br />}<br /><br />function cleanvalue(id) {<br />  var x = el(id);<br />  x.value = x.value.replace(/^\s*/, '').replace(/\n\s*/g, '\n');<br />}<br /><br /><br />  func
tion loadXMLDoc(url) {<br />    //
 branch for native XMLHttpRequest object<br />    if (window.XMLHttpRequest) {<br />        req = new XMLHttpRequest();<br />        req.onreadystatechange = processReqChange;<br />        req.open(&quot;GET&quot;, &quot;proxy?remotehost=&quot;+url, true); // the third parameter - true - specified ASYNCHRONOUS processing i.e. not waiting for the response!<br />        req.send(null);<br />    // branch for IE/Windows ActiveX version<br />    } else if (window.ActiveXObject) {<br />        req = new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);<br />        if (req) {<br />            req.onreadystatechange = processReqChange;<br />            req.open(&quot;GET&quot;, &quot;proxy?remotehost=&quot;+url, true); // the third parameter - true - specified ASYNCHRONOUS processing i.e. not waiting for the response!<br />            req.send();<br />        }<br />    }<br />  }        <br />  function processReqChange()  {<br />    // only if req shows &quot;complete&quot;<br />    if (req.readyState == 4) {<br />      // only if &quot;OK&quot;<br />      if (req.status == 200) {<br />          xmldoc=req.responseXML;<br />          document.xmlform.xml.value=req.responseText;<br />      }<br />      else {<br />        alert(&quot;Error: can not get the content (&quot;+ req.statusText +&quot;)&quot;);<br />      }    <br />    }<br />  }<br />        <br />  &lt;/SCRIPT&gt;<br />  &lt;/head&gt;<br />  &lt;body onload=&quot;loadXMLDoc('news.bbc.co.uk/rss/newsonline_world_edition/front_page/rss091.xml');&quot;&gt;<br />  &lt;a href=&quot;javascript:loadXMLDoc('news.bbc.co.uk/rss/newsonline_world_edition/front_page/rss091.xml');&quot;&gt;BBC World News&lt;/a&gt;<br />  &lt;a href=&quot;javascript:loadXMLDoc('www.orablogs.com/orablogs/rss.xsql');&quot;&gt;OraBlogs&lt;/a&gt;<br /><br />    &lt;form name=&quot;xmlform&quot; onsubmit=&quot;test_xslt();return false&quot;&gt;<br />      &lt;table&gt;<br />        &lt;tr&gt;<br />          &lt;td&gt;RSS Feed&lt;br /&gt;<br />            &lt;textarea id=&quot;xml&quot; cols=&quot;40&quot; rows=&quot;10&quot;&gt;<br />              &lt;page&gt;<br />                &lt;message&gt;<br />                  Hello World.<br />                &lt;/message&gt;<br />              &lt;/page&gt;<br />            &lt;/textarea&gt;<br />          &lt;/td&gt;<br />          &lt;td&gt;XSLT &lt;br /&gt;<br />            &lt;textarea id=&quot;xslt&quot; cols=&quot;40&quot; rows=&quot;10&quot;&gt;<br />&lt;xsl:stylesheet&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;rss/channel/title/.&quot;/&gt;<br />     &lt;/div&gt;<br />     &lt;ul&gt;<br />     &lt;xsl:for-each select=&quot;rss/channel/item&quot;&gt;<br />       &lt;li&gt;<br />         &lt;xsl:element name=&quot;a&quot;&gt;<br />           &lt;xsl:attribute name=&quot;target&quot;&gt;&lt;xsl:text&gt;_blank&lt;/xsl:text&gt;&lt;/xsl:attribute&gt;<br />           &lt;xsl:attribute name=&quot;href&quot;&gt;&lt;xsl:value-of select=&quot;link&quot; /&gt;&lt;/xsl:attribute&gt;<br />           &lt;xsl:value-of select=&quot;title&quot; /&gt;<br />         &lt;/xsl:element&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;            &lt;/textarea&gt;<br />          &lt;/td&gt;<br />        &lt;/tr&gt;<br />        &lt;tr&gt;<br />          &lt;td colspan=&quot;2&quot;&gt;<br />            &lt;input type=&quot;submit&quot; value=&quot;process&quot;/&gt;<br />          &lt;/td&gt;<br />        &lt;/tr&gt;<br />        &lt;tr&gt;<br />          &lt;td valign=&quot;top&quot;&gt;<br />            XML Result&lt;br /&gt;<br />            &lt;textarea id=&quot;html&quot; cols=&quot;40&quot; rows=&quot;10&quot;&gt;<br />            &lt;/textarea&gt;<br />          &lt;/td&gt;<br />          &lt;td&gt;<br />            &lt;div id=&quot;htmldisplay&quot;&gt;&lt;/div&gt;<br />          &lt;/td&gt;<br />        &lt;/tr&gt;<br />      &lt;/table&gt;<br />    &lt;/form&gt;<br />  &lt;/body&gt;<br />&lt;/html&gt;<br />&nbsp;

When I click on the OraBlogs RSS Feed link, an Ajax request is made to the AjaxProxyServlet, passing a parameter called remotehost that contains the URL at which the OraBlogs RSS Feed can be found. The Proxy Servlet makes the request to get hold of the RSS feed (an XML Document) and sends it back to the browser. It is received in the ResponseXML on the HttpXmlRequest object and put in the textarea. When the Process button is pressed, the XML is read and parsed, the XSLT from the second textarea is read and parsed, then the XSLT transformation is performed and the resulting XML is put into the third textarea. Subsequently, the resulting XML is taken and placed in the innerHtml property of a DIV element:  

el(‘htmldisplay’).innerHTML = html;

Resources

Source code for the examples in this article: AjaXsltRss.zip.

Homepage for the Google Code AJAXSLT project

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.

2 Comments