Attachment 'snXSLT.js'
Download 1 /*
2 * snXSLT.js: JavaScript Client-side XSLT Processor
3 * Copyright (C) 2003-2005 Yoon, Sang-Min <mail@sixmen.pe.kr>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 /*
20 * This supports IE 6.0(exactly MSXML 3.0) and Mozilla 1.2 or above
21 *
22 * v1.1: support 'xml-stylesheet processing instruction'
23 * v1.0: initial publish
24 */
25 /*
26 * Usage:
27 * snXSLT.transform(xmlstr, xslstr, params, div)
28 * - 'xmlstr' and 'xslstr' are either a URL or an XML document
29 * ex) "http://foo.bar/foobar.xml", "<?xml version='1.0'?><a/>"
30 * - if 'xslstr' is null, try 'xml-stylesheet processing instruction'
31 * - 'params' is parameters of XSLT processing, array of names and values
32 * ex) ['name1','value1','name2','value2']
33 * - 'div' is the target element
34 */
35 /*
36 * Example:
37 *
38 * <div id="result"></div>
39 * <script language="JavaScript" src="snXSLT.js"></script>
40 * <script language="JavaScript">
41 * window.onload = function()
42 * {
43 * var xml = "<?xml version='1.0'?><a/>";
44 * var xsl = "<?xml version='1.0'?>"
45 * + "<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>"
46 * + "<xsl:template match='/'><h2>It works!!</h2></xsl:template>"
47 * + "</xsl:stylesheet>";
48 * var div = document.getElementById('result');
49 * var mysnXSLT = new snXSLT();
50 * mysnXSLT.transform(xml,xsl,null,div);
51 * }
52 * </script>
53 */
54
55 // TODO: better detection
56 // http://www.webreference.com/tools/browser/javascript.html
57 var agt = navigator.userAgent.toLowerCase();
58 var isOpera = (agt.indexOf("opera")!=-1);
59 var isIE = (agt.indexOf("msie")!=-1 && !isOpera);
60 var isMoz = document.implementation && document.implementation.createDocument;
61
62 function snXSLT()
63 {
64 // is 'str' a XML string or a XML filename?
65 function isXML(str)
66 {
67 return str.substring(0,5)=="<?xml";
68 }
69
70 // find xml-stylesheet PI
71 function getStyleSheetFromXML(xml) // {{{
72 {
73 var children = xml.childNodes;
74 for(var i=0;i<children.length;i++)
75 {
76 var node = children.item(i);
77 if(node.nodeType==7 && node.nodeName=='xml-stylesheet')
78 {
79 var start = node.data.indexOf('href=');
80 if(start!=-1)
81 {
82 start += 5;
83 var ch = node.data.charAt(start);
84 if(ch=='"')
85 start++;
86 else if(ch=="'")
87 start++;
88 else
89 ch = ' ';
90 var end = node.data.indexOf(ch, start);
91 if(end!=-1)
92 {
93 return node.data.substr(start, end-start);
94 }
95 }
96 }
97 }
98 return "";
99 } // }}}
100
101 this.transform = function(xmlstr, xslstr, params, div)
102 {
103 alert("Your browser does not support XSLT.");
104 }
105
106 if(isIE) // {{{
107 {
108 function reportParseError(error, div) // {{{
109 {
110 var r = "<b><p><big>Error while loading '";
111 r += error.url + "'</big></p>";
112 r += "<p>" + error.reason + "</p></b>";
113 if(error.line>0)
114 {
115 r += "<xmp>";
116 r += "at line " + error.line;
117 r += ", character " + error.linepos + "\n";
118 r += error.srcText + "\n";
119 {
120 var s = "";
121 for(var i=1;i<error.linepos;i++) s += " ";
122 r += s + "^";
123 }
124 r += "</xmp>";
125 }
126 div.innerHTML = r;
127 } // }}}
128
129 function reportRuntimeError(exception, div) // {{{
130 {
131 var r = "<b><p><big>Error while XSLT processing</big></p>";
132 r += "<p>" + exception.description + "</p></b>";
133 div.innerHTML = r;
134 } // }}}
135
136 function readXMLIE(str, div) // {{{
137 {
138 var xml = new ActiveXObject('MSXML2.FreeThreadedDOMDocument');
139 xml.async = false;
140 if(isXML(str))
141 xml.loadXML(str);
142 else
143 xml.load(str);
144 if(xml.parseError.errorCode!=0)
145 {
146 reportParseError(xml.parseError, div);
147 return null;
148 }
149 return xml;
150 } // }}}
151
152 this.transform = function(xmlstr, xslstr, params, div) // {{{
153 {
154 window.status = 'Waiting....';
155
156 var xml = readXMLIE(xmlstr, div);
157 if(xml==null)
158 {
159 window.status = '';
160 return;
161 }
162
163 if(xslstr==null || xslstr.length==0)
164 {
165 xslstr = getStyleSheetFromXML(xml);
166 }
167
168 var xsl = readXMLIE(xslstr, div);
169 if(xsl==null)
170 {
171 window.status = '';
172 return;
173 }
174
175 try
176 {
177 var xslt = new ActiveXObject('MSXML2.XSLTemplate');
178 xslt.stylesheet = xsl;
179
180 var xsltproc = xslt.createProcessor();
181 xsltproc.input = xml;
182 if(params)
183 {
184 for(var i=0;i<params.length/2;i++)
185 {
186 var name = params[i*2];
187 var value = params[i*2+1];
188 xsltproc.addParameter(name, value);
189 }
190 }
191 xsltproc.transform();
192 }
193 catch(exception)
194 {
195 reportRuntimeError(exception, div);
196 window.status = '';
197 return;
198 }
199 div.innerHTML = xsltproc.output;
200
201 window.status = '';
202 } // }}}
203 } // }}}
204
205 if(isMoz) // {{{
206 {
207 /* for Mozilla < 1.2
208 this._set_params = function() // {{{
209 {
210 if(_params)
211 {
212 paramnodes = _xsl.getElementsByTagName("param");
213 for(i=0;i<_params.length/2;i++)
214 {
215 name = _params[i*2];
216 value = _params[i*2+1];
217 for(j=0;j<paramnodes.length;j++)
218 {
219 attrs = paramnodes[j].attributes;
220 paramname = attrs.getNamedItem("name").nodeValue;
221 if(paramname==name)
222 {
223 newval = "'" + value + "'";
224 attrs.getNamedItem("select").nodeValue = newval;
225 break;
226 }
227 }
228 }
229 }
230 } // }}}
231 */
232
233 function readXMLMoz(str) // {{{
234 {
235 if(isXML(str))
236 {
237 var xml = document.implementation.createDocument("", "", null);
238 var domParser = new DOMParser();
239 xml = domParser.parseFromString(str, 'text/xml');
240 return xml;
241 }
242 else
243 {
244 var req = new XMLHttpRequest();
245 req.open("GET", str, false);
246 req.overrideMimeType("text/xml");
247 req.send(null);
248 return req.responseXML;
249 }
250 } // }}}
251
252 this.transform = function(xmlstr, xslstr, params, div) // {{{
253 {
254 try
255 {
256 window.status = 'Waiting....';
257
258 var xml = readXMLMoz(xmlstr);
259
260 if(xslstr==null || xslstr.length==0)
261 {
262 xslstr = getStyleSheetFromXML(xml);
263 }
264
265 var xsl = readXMLMoz(xslstr);
266
267 var xsltproc = new XSLTProcessor();
268 xsltproc.importStylesheet(xsl);
269 if(params)
270 {
271 for(i=0;i<params.length/2;i++)
272 {
273 var name = params[i*2];
274 var value = params[i*2+1];
275 xsltproc.setParameter(null, name, value);
276 }
277 }
278 var out = xsltproc.transformToDocument(xml);
279
280 // TODO: better way?
281 div.innerHTML = '';
282 div.appendChild(out.firstChild);
283 div.innerHTML = div.innerHTML; // for awareness of tags
284 }
285 catch(exception)
286 {
287 div.innerHTML = exception;
288 }
289 finally
290 {
291 window.status = '';
292 }
293 } // }}}
294 } // }}}
295 }
296
297 // vim:enc=utf-8:sts=0:ts=4:noexpandtab:foldmethod=marker
298
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.