
AJAX + XML + XSLT or a new look at AJAX
A year and a half ago, there was a problem in the dynamic generation of HTML code. It is too cumbersome to rebuild HTML using the DOM and the code turns out to be large, loading the generated HTML on the server is not a very beautiful and hard-core solution.
It was decided to look for an alternative way to generate HTML.
And it was found: AJAX + XML + XSLT.
On the server side is an XSLT template, a script that generates XML (or an XML file). On the client side, XML and XSLT are loaded using AJAX and converted to HTML
Example usage:
myDiv - the element into which our HTML
callback will be inserted - a callback function that will work after loading the content.
Pros: Minimization of downloaded data from the server, unloading the server when generating HTML, in this case, this operation is transferred to the client’s computer.
Cons of this method: Opera and Safari do not work correctly when parsing XSLT templates containing complex instructions such as for-each
Well, the code itself that implements this bundle:
It was decided to look for an alternative way to generate HTML.
And it was found: AJAX + XML + XSLT.
On the server side is an XSLT template, a script that generates XML (or an XML file). On the client side, XML and XSLT are loaded using AJAX and converted to HTML
Example usage:
- function getContent(){
- try{
- var xslt = new xsltLoad("http://"+location.hostname+"/content.xml, "http://"+location.hostname+"/template.xsl", "myDiv", callback);
- xslt.init();
- }catch(e){
- alert(e.message)
- }
- }
* This source code was highlighted with Source Code Highlighter.
myDiv - the element into which our HTML
callback will be inserted - a callback function that will work after loading the content.
Pros: Minimization of downloaded data from the server, unloading the server when generating HTML, in this case, this operation is transferred to the client’s computer.
Cons of this method: Opera and Safari do not work correctly when parsing XSLT templates containing complex instructions such as for-each
Well, the code itself that implements this bundle:
- function getXmlHttpRequestObject() {
- if (window.XMLHttpRequest) {
- try {
- var receiveReq = new XMLHttpRequest();
- } catch (e){}
- } else if (window.ActiveXObject) {
- var xmlHttpVers = new Array('Msxml2.XMLHTTP.6.0','Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0','Msxml2.XMLHTTP','Microsoft.XMLHTTP');
- for(var i = 0; i < xmlHttpVers.length && !receiveReq; i++){
- try{
- var receiveReq = new ActiveXObject(xmlHttpVers[i]);
- }catch(e){}
- }
- }
- return receiveReq;
- }
-
- function __callXmlHttprequest(r, f){
- if(r.readyState == 4 && r.status == 200){
- f(r);
- }
- }
-
- function getReceive(url, funcCallback, l){
- if(!l){
- l = true;
- }
- var xmlHttp = getXmlHttpRequestObject();
- if(!xmlHttp){
- return;
- }
- if(funcCallback){
- xmlHttp.onreadystatechange = function(){
- __callXmlHttprequest(xmlHttp, funcCallback);
- };
- }else{
- xmlHttp.onreadystatechange = function(){};
- }
- xmlHttp.open("GET", url, l);
- xmlHttp.send(null);
- }
-
- var Browser = {
- IE: !!(window.attachEvent && !window.opera),
- Opera: !!window.opera,
- Khtml: navigator.userAgent.indexOf('AppleWebKit/') > -1,
- Gecko: navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1
- }
-
- function xsltLoad(xmlFile, xsltFile, elementId, _call){
- var method;
- var postData;
- var stylesheetDoc;
- var xmldoc, xsldoc;
-
- this.createMSDOM = function(){
- var msdom;
- var v = new Array("Msxml2.DOMDocument.6.0","Msxml2.DOMDocument.5.0","Msxml2.DOMDocument.4.0");
- for (var i=0; i< v.length && !msdom; i++) {
- try {
- msdom = new ActiveXObject(v[i]);
- } catch (e) {}
- }
- return msdom;
- }
-
- this.init = function(){
- if(Browser.IE){
- xmldoc = new ActiveXObject("Microsoft.XMLDOM");
- xmldoc.async = false;
- xmldoc.load(xmlFile);
-
- xsldoc = new ActiveXObject("Microsoft.XMLDOM");
- xsldoc.async = false;
- xsldoc.load(xsltFile);
- document.getElementById(elementId).innerHTML = xmldoc.transformNode(xsldoc);
- if(_call){
- _call();
- }
- }else{
- this.loadStylesheet();
- this.loadXMLFile();
- return;
- }
- }
- this.callBack = function(r){
- try{
- var dp = new DOMParser();
- stylesheetDoc = dp.parseFromString(r.responseText, "text/xml");
- }catch(e){
- alert(e)
- }
- }
-
- this.loadStylesheet = function(){
- getReceive(xsltFile+"?"+randomNumber(1,999999), this.callBack, false);
- }
-
- this.loadXMLFile = function(){
- if(!method){
- getReceive(xmlFile, this.buildHTML);
- }else{
- getReceive(xmlFile, encodeURIComponent(this.postData), this.buildHTML);
- }
- }
-
- this.buildHTML = function(r){
- xmlResponse = r.responseXML;
- try{
- var xsltProcessor = new XSLTProcessor();
- xsltProcessor.importStylesheet(stylesheetDoc);
- page = xsltProcessor.transformToFragment(xmlResponse, document);
- var e = document.getElementById(elementId);
- e.innerHTML = "";
- e.appendChild(page);
- stylesheetDoc = null;
- xsltProcessor = null;
- }catch(e2){
- alert(e2.message);
- }
- if(_call){
- _call();
- }
- }
- }
* This source code was highlighted with Source Code Highlighter.