📜 ⬆️ ⬇️

AJAX + XML + XSLT or a new lookup on AJAX

A year and a half ago, there was a problem in dynamically generating HTML code. To rebuild HTML using DOM is too cumbersome and the code turns out to be big, loading the generated HTML on the server is not a very nice and harsh 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 the XSLT template, the script that generates the XML (or XML file). On the client side via AJAX, XML and XSLT are loaded and converted to HTML

Example of use:
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 .
  1. 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 .
  2. 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 .
  3. 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 .
  4. 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 .
  5. 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 .
  6. 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 .
  7. 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 .
  8. 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 .
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 will be inserted
callback is a callback function that will work after the content is loaded.
Pros: Minimizing the download data from the server, unloading the server when generating HTML, in this case, this operation is transferred to the client's computer.
Disadvantages of this method: it does not work correctly in Opera and Safari browsers when parsing XSLT templates containing complex for-each instructions

Well, the code itself that implements this bundle:
  1. function getXmlHttpRequestObject () {
  2. if (window.XMLHttpRequest) {
  3. try {
  4. var receiveReq = new XMLHttpRequest ();
  5. } catch (e) {}
  6. } else if (window.ActiveXObject) {
  7. var xmlHttpVers = new Array ( 'Msxml2.XMLHTTP.6.0' , 'Msxml2.XMLHTTP.5.0' , 'Msxml2.2.XMLHTTP.4.0' , 'Msxml2.XMLHTTP.3.0' , 'Msxml2.XMLHTTP' , 'Microsoft.XMLHTTP' ) ;
  8. for ( var i = 0; i <xmlHttpVers.length &&! receiveReq; i ++) {
  9. try {
  10. var receiveReq = new ActiveXObject (xmlHttpVers [i]);
  11. } catch (e) {}
  12. }
  13. }
  14. return receiveReq;
  15. }
  16. function __callXmlHttprequest (r, f) {
  17. if (r.readyState == 4 && r.status == 200) {
  18. f (r);
  19. }
  20. }
  21. function getReceive (url, funcCallback, l) {
  22. if (! l) {
  23. l = true ;
  24. }
  25. var xmlHttp = getXmlHttpRequestObject ();
  26. if (! xmlHttp) {
  27. return ;
  28. }
  29. if (funcCallback) {
  30. xmlHttp.onreadystatechange = function () {
  31. __callXmlHttprequest (xmlHttp, funcCallback);
  32. };
  33. } else {
  34. xmlHttp.onreadystatechange = function () {};
  35. }
  36. xmlHttp.open ( "GET" , url, l);
  37. xmlHttp.send ( null );
  38. }
  39. var Browser = {
  40. IE: !! (window.attachEvent &&! Window.opera),
  41. Opera: !! window.opera,
  42. Khtml: navigator.userAgent.indexOf ( 'AppleWebKit /' )> -1,
  43. Gecko: navigator.userAgent.indexOf ( 'Gecko' )> -1 && navigator.userAgent.indexOf ( 'KHTML' ) == -1
  44. }
  45. function xsltLoad (xmlFile, xsltFile, elementId, _call) {
  46. var method;
  47. var postData;
  48. var stylesheetDoc;
  49. var xmldoc, xsldoc;
  50. this .createMSDOM = function () {
  51. var msdom;
  52. var v = new Array ( "Msxml2.DOMDocument.6.0" , "Msxml2.DOMDocument.5.0" , "Msxml2.DOMDocument.4.0" );
  53. for ( var i = 0; i <v.length &&! msdom; i ++) {
  54. try {
  55. msdom = new ActiveXObject (v [i]);
  56. } catch (e) {}
  57. }
  58. return msdom;
  59. }
  60. this .init = function () {
  61. if (Browser.IE) {
  62. xmldoc = new ActiveXObject ( "Microsoft.XMLDOM" );
  63. xmldoc.async = false ;
  64. xmldoc.load (xmlFile);
  65. xsldoc = new ActiveXObject ( "Microsoft.XMLDOM" );
  66. xsldoc.async = false ;
  67. xsldoc.load (xsltFile);
  68. document .getElementById (elementId) .innerHTML = xmldoc.transformNode (xsldoc);
  69. if (_call) {
  70. _call ();
  71. }
  72. } else {
  73. this .loadStylesheet ();
  74. this .loadXMLFile ();
  75. return ;
  76. }
  77. }
  78. this .callBack = function (r) {
  79. try {
  80. var dp = new DOMParser ();
  81. stylesheetDoc = dp.parseFromString (r.responseText, "text / xml" );
  82. } catch (e) {
  83. alert (e)
  84. }
  85. }
  86. this .loadStylesheet = function () {
  87. getReceive (xsltFile + "?" + randomNumber (1.999999), this .callBack, false );
  88. }
  89. this .loadXMLFile = function () {
  90. if (! method) {
  91. getReceive (xmlFile, this .buildHTML);
  92. } else {
  93. getReceive (xmlFile, encodeURIComponent ( this .postData), this .buildHTML);
  94. }
  95. }
  96. this .buildHTML = function (r) {
  97. xmlResponse = r.responseXML;
  98. try {
  99. var xsltProcessor = new XSLTProcessor ();
  100. xsltProcessor.importStylesheet (stylesheetDoc);
  101. page = xsltProcessor.transformToFragment (xmlResponse, document );
  102. var e = document .getElementById (elementId);
  103. e.innerHTML = "" ;
  104. e.appendChild (page);
  105. stylesheetDoc = null ;
  106. xsltProcessor = null ;
  107. } catch (e2) {
  108. alert (e2.message);
  109. }
  110. if (_call) {
  111. _call ();
  112. }
  113. }
  114. }
* This source code was highlighted with Source Code Highlighter .


')

Source: https://habr.com/ru/post/51876/


All Articles