Created
September 23, 2009 16:19
-
-
Save thinkerbot/192095 to your computer and use it in GitHub Desktop.
XSLT formatter in javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Adapted from: http://www.w3schools.com/xsl/xsl_client.asp | |
Xslt: { | |
// Transforms xml using the specified xsl and returns a document fragment. | |
// The fragment may be added to the DOM using jQuery like this: | |
// | |
// node = Xslt.transform("/example.xml", "/example.xsl"); | |
// jQuery("#node_id").replaceWith(node); | |
// | |
transform: function(xml_url, xsl_url) { | |
xml = this.load_xml(xml_url); | |
xsl = this.load_xml(xsl_url); | |
// code for IE | |
if (window.ActiveXObject) { | |
return xml.transformNode(xsl); | |
} | |
// code for Mozilla, Firefox, Opera, etc. | |
else if (document.implementation && document.implementation.createDocument) { | |
xsltProcessor = new XSLTProcessor(); | |
xsltProcessor.importStylesheet(xsl); | |
return xsltProcessor.transformToFragment(xml, document); | |
} | |
else { | |
alert('Your browser cannot use this script. Try Firefox or Opera.'); | |
} | |
}, | |
// Loads an xml document from the url. | |
load_xml: function(url) { | |
var doc; | |
// code for IE | |
if (window.ActiveXObject) { | |
doc = new ActiveXObject("Microsoft.XMLDOM"); | |
} | |
// code for Mozilla, Firefox, Opera, etc. | |
else if (document.implementation && document.implementation.createDocument) { | |
doc = document.implementation.createDocument("","",null); | |
} | |
else { | |
alert('Your browser cannot use this script. Try Firefox or Opera.'); | |
} | |
doc.async = false; | |
doc.load(url); | |
return(doc); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment