/
jquery.xslt.js
122 lines (111 loc) · 3.4 KB
/
jquery.xslt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* @VERSION@
*
* Copyright (c) 2008-2009 Chris Thatcher (claypooljs.com)
* Dual licensed under the MIT (MIT-LICENSE.txt)
* and GPL (GPL-LICENSE.txt) licenses.
*
* jQuery.jsPath XSLT
*
* based on jquery.xslt.js by Johann Burkard (<mailto:jb@eaio.com>)
*
* NOTE: This plugin is primarily designed for xml to javascript
* conversion of legacy data that can't be delivered as json.
* it has many other possible uses. it is very fast as very
* efficient, and works in all modern browsers
*
*/
(function($) {
var _xslt = null,
template = null,
processor = null;
$.xslt = function() {
return this;
}
var str = /^\s*</;
if (document.recalc) { // IE 5+
$.xslt = function(name, xslt_url, parameters, filter) {
// Load your XSL
//alert("Loading "+xslt_url);
_xslt = new ActiveXObject("MSXML2.FreeThreadedDomDocument");
_xslt.async = false;
_xslt.load(xslt_url);
// create a compiled XSL-object
template = new ActiveXObject("MSXML2.XSLTemplate");
template.stylesheet = _xslt.documentElement;
// create XSL-processor
processor = template.createProcessor();
// input for XSL-processor
if(parameters){
for(var p in parameters){
processor.addParameter(p, parameters[p]);
}
}
//add function to jquery namespace
$[name] = function(xml){
processor.input = xml;
processor.transform();
if(filter && $.isFunction(filter)){
return filter(processor.output);
}else{
return processor.output;
}
};
return this;
};
}else if ( window.DOMParser != undefined &&
window.XMLHttpRequest != undefined &&
window.XSLTProcessor != undefined ) { // Mozilla 0.9.4+, Opera 9+
var processor = new XSLTProcessor();
var support = false;
if ($.isFunction(processor.transformDocument)) {
support = window.XMLSerializer != undefined;
} else {
support = true;
}
if (support) {
$.xslt = function(name, xslt_url, parameters, filter) {
//compile an xslt and add it to the jQuery static namespace
$.ajax({
url:xslt_url,
type:"GET",
dataType:'xml',
async:false,
success: function(xml){
_xslt = xml;
}
});
processor.importStylesheet(_xslt);
if(parameters){
for(var p in parameters){
processor.setParameter(null, p, parameters[p]);
}
}
//add the function to jquery namespace
if ($.isFunction(processor.transformToFragment)) {
$[name] = function(xml){
var result;
if(filter && $.isFunction(filter)){
result = processor.transformToFragment(xml, document);
return filter(result);
}else{
return processor.transformToFragment(xml, document);
}
};
} else if($.isFunction(processor.transformDocument)) {
$[name] = function(xml){
// obsolete Mozilla interface
var resultDoc = document.implementation.createDocument("", "", null);
processor.transformDocument(xml, _xslt, resultDoc, null);
if(filter && $.isFunction(filter)){
return filter($("#transformiixResult",resultDoc).html());
}else{
return $("#transformiixResult",resultDoc).html();
}
};
}
return this;
};
}
}
})(jQuery);