/*
Title:
xml transformer
Description:
This is a Greasemonkey user script for Firefox.
This script applies XSLT on plain XML files
*/
// ==UserScript==
// @name xml transformer
// @namespace http://sample.com
// @description apply XSLT on plain XML
// @include http://localhost/*.xml
// ==/UserScript==
var contentType = 'text/html';
detectFormats();
function detectFormats() {
// define classNames and URLs of corresponding XSLT files - edit this to add new formats
var b = new Array();
b[0] = new Array('Root1', 'http://localhost/gmonkey/xslt/root1.xslt', 'text/html');
b[1] = new Array('Root2', 'http://localhost/gmonkey/xslt/root2.xslt', 'text/html');
// look for certain signature in document
for (i=0; i<b.len; i++) {
var schemaType = b[i][0];
if (document.getElementsByTagName(schemaType).length > 0){
xsltURL = b[i][1];
contentType = b[i][2];
GM_log(xsltURL);
fetchXSLT(xsltURL);
}
}
}
function fetchXSLT(url){
GM_xmlhttpRequest({
method: "GET",
url: url,
headers: {
'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
'Accept': 'application/atom+xml,application/xml,text/xml',
'Cache-Control': 'no-cache',
},
onload: function(response) {
var xslt = new DOMParser().parseFromString(response.responseText, "text/xml");
transformXML(xslt);
}
});
}
// transform the document to RDF using the XSLT file
function transformXML(xslt){
var processor = new XSLTProcessor();
processor.importStylesheet(xslt);
var dataXML = document;
var processed = processor.transformToDocument(dataXML);
var dataString = (new XMLSerializer()).serializeToString(processed);
location.href = 'data:' + contentType + ',' + dataString;
}
2006-03-06
My first grease monkey script
I wrote my first grease monkey script today. It is to apply a XSLT style sheet on certain types of XML files. This is because usually the xml contains too much information and I just want ot view certain part of it and in more clean format such as a table. Here is the code:
Subscribe to:
Post Comments (Atom)
1 comment:
Hey thanks for your post. Gave me a good start. Here is another way to do it.
Post a Comment