/doc/jquery-xslt/jquery.xslt.js

http://refurbishment-database.googlecode.com/ · JavaScript · 122 lines · 81 code · 9 blank · 32 comment · 27 complexity · 7ab5ac357db51a772a28a344da6c70d6 MD5 · raw file

  1. /*
  2. * jquery.xslt.js
  3. *
  4. * Copyright (c) 2005-2008 Johann Burkard (<mailto:jb@eaio.com>)
  5. * <http://eaio.com>
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included
  15. * in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  20. * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  21. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  23. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. */
  26. /**
  27. * jQuery client-side XSLT plugins.
  28. *
  29. * @author <a href="mailto:jb@eaio.com">Johann Burkard</a>
  30. * @version $Id: jquery.xslt.js,v 1.10 2008/08/29 21:34:24 Johann Exp $
  31. */
  32. (function($) {
  33. $.fn.xslt = function() {
  34. return this;
  35. }
  36. var str = /^\s*</;
  37. if (document.recalc) { // IE 5+
  38. $.fn.xslt = function(xml, xslt) {
  39. var target = $(this);
  40. var change = function() {
  41. var c = 'complete';
  42. if (xm.readyState == c && xs.readyState == c) {
  43. window.setTimeout(function() {
  44. target.html(xm.transformNode(xs.XMLDocument));
  45. }, 50);
  46. }
  47. };
  48. var xm = document.createElement('xml');
  49. xm.onreadystatechange = change;
  50. xm[str.test(xml) ? "innerHTML" : "src"] = xml;
  51. var xs = document.createElement('xml');
  52. xs.onreadystatechange = change;
  53. xs[str.test(xslt) ? "innerHTML" : "src"] = xslt;
  54. $('body').append(xm).append(xs);
  55. return this;
  56. };
  57. }
  58. else if (window.DOMParser != undefined && window.XMLHttpRequest != undefined && window.XSLTProcessor != undefined) { // Mozilla 0.9.4+, Opera 9+
  59. var processor = new XSLTProcessor();
  60. var support = false;
  61. if ($.isFunction(processor.transformDocument)) {
  62. support = window.XMLSerializer != undefined;
  63. }
  64. else {
  65. support = true;
  66. }
  67. if (support) {
  68. $.fn.xslt = function(xml, xslt) {
  69. var target = $(this);
  70. var transformed = false;
  71. var xm = {
  72. readyState: 4
  73. };
  74. var xs = {
  75. readyState: 4
  76. };
  77. var change = function() {
  78. if (xm.readyState == 4 && xs.readyState == 4 && !transformed) {
  79. var processor = new XSLTProcessor();
  80. if ($.isFunction(processor.transformDocument)) {
  81. // obsolete Mozilla interface
  82. resultDoc = document.implementation.createDocument("", "", null);
  83. processor.transformDocument(xm.responseXML, xs.responseXML, resultDoc, null);
  84. target.html(new XMLSerializer().serializeToString(resultDoc));
  85. }
  86. else {
  87. processor.importStylesheet(xs.responseXML);
  88. resultDoc = processor.transformToFragment(xm.responseXML, document);
  89. target.empty().append(resultDoc);
  90. }
  91. transformed = true;
  92. }
  93. };
  94. if (str.test(xml)) {
  95. xm.responseXML = new DOMParser().parseFromString(xml, "text/xml");
  96. }
  97. else {
  98. xm = $.ajax({ dataType: "xml", url: xml});
  99. xm.onreadystatechange = change;
  100. }
  101. if (str.test(xslt)) {
  102. xs.responseXML = new DOMParser().parseFromString(xslt, "text/xml");
  103. change();
  104. }
  105. else {
  106. xs = $.ajax({ dataType: "xml", url: xslt});
  107. xs.onreadystatechange = change;
  108. }
  109. return this;
  110. };
  111. }
  112. }
  113. })(jQuery);