/ Published in: JavaScript
URL: http://bcmoney-mobiletv.com/blog/2010/12/08/js-transformer/
KEEP IN MIND THE SAME-ORIGIN POLICY OF MOST BROWSERS... you'll probably have to use some server-side code to make this more useful, but on its own it will translate XML based on your prescribed XSLT.
It uses HTML, JavaScript and XSLT (optionally you could add some CSS to make it look even prettier) to render an XML file into HTML, JSON, CSV, or just about anything else you decide. Just feed in an XML file and XSL file as URL parameters.
Expand |
Embed | Plain Text
<html> <head> <title>JS Transformer</title> <script type="text/javascript"> /* * loadXMLDoc * Loads an XML Document from file or via a web-based URL * *@param docURL String location of the file to retrieve and load as XML *@return xhttp.responseXML DOM representation of the text *@author W3schools *@source http://www.w3schools.com/DOM/loadxmldoc.js */ function loadXMLDoc(docURL) { if (window.XMLHttpRequest) { xhttp=new XMLHttpRequest(); } else { xhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xhttp.open("GET",docURL,false); xhttp.send(""); return xhttp.responseXML; } /* * loadXMLString * Loads an XML Document from a hard-coded string, or, as a result of loading plaintext * *@param txt String the text content to load as XML *@return xmlDoc DOM representation of the text *@author W3schools *@source http://www.w3schools.com/DOM/loadxmldoc.js */ function loadXMLString(txt) { if (window.DOMParser) { // FireFox, Opera, Chrome, Safari parser=new DOMParser(); xmlDoc=parser.parseFromString(txt,"text/xml"); } else { // Internet Explorer xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async = "false"; xmlDoc.loadXML(txt); } return xmlDoc; } /* * gup * gup stands for 'Get URL Paramaters' (values retrieved by name) * * Usage: * Your URL looks like "http://site.com/service?user=frank&favcolor=green * var user_param = gup( 'user' ); * var background = gup( 'favcolor' ); * "user_param" would be set to String "frank", "background" would be set to "green" * *@param name String the name of the parameter to get the value of *@return results String representation of the text in the URL parameter *@author Justin Barlow *@source http://www.netlobo.com/url_query_string_javascript.html * From the author: * "Most of the server-side programming languages that I know of like PHP, ASP, or JSP give you easy access to parameters in the query string of a URL. Javascript does not give you easy access. With javascript you must write your own function to parse the window.location.href value to get the query string parameters you want. Here is a small function I wrote that will parse the window.location.href value and return the value for the parameter you specify. It does this using javascript's built in regular expressions" */ function gup( name ) { name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); var regexS = "[\\?&]"+name+"=([^&#]*)"; var regex = new RegExp( regexS ); var results = regex.exec( window.location.href ); if( results == null ) return ""; else return results[1]; } x = gup("xml"); var xmlFile = (typeof x != "undefined" && x !== null && x != "") ? x : "data.xml"; s = gup("xsl"); var xslFile = (typeof s != "undefined" && s !== null && s != "") ? s : "data2html.xsl"; /** * displayResult * Process XML using specified XSL Transformations, then display the result in an HTML page * * NOTE: Relies on the browser's internal XSLT processor, * which should be fairly standard, but you might * want to expect bugs with old browsers * *@author copelandb */ function displayResult() { xml=loadXMLDoc(xmlFile); xsl=loadXMLDoc(xslFile); if (window.ActiveXObject) // IE { ex=xml.transformNode(xsl); document.getElementById("result").innerHTML=ex; } else if (document.implementation && document.implementation.createDocument) // Mozilla, Firefox, Opera, Chrome, Safari, etc. { xsltProcessor=new XSLTProcessor(); xsltProcessor.importStylesheet(xsl); resultDocument = xsltProcessor.transformToFragment(xml,document); document.getElementById("result").appendChild(resultDocument); } } </script> <style type="text/css"> html { font-size:100.01%; } body { font-size: 75%; color: #000; background: #ffffff; font-family: "Helvetica Neue", Arial, Helvetica, sans-serif line-height:1; } /* your style here */ div#result { display:block; } </style> <!-- external and additional stylesheets can easily be applied to result <link href="http://example.com/printer-stylesheet.css" type="text/css" rel="@print" /> --> </head> <body onload="displayResult()"> <div id="result"> </div> </body> </html>
You need to login to post a comment.
