DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Some Javascript Usefull Functions For Xml/xslt

09.07.2005
| 12629 views |
  • submit to reddit
        works well on IE/firefox ... (need to be adapted)
		function getDomFromFile(file) {
			// Load XML
			if (typeof ActiveXObject != 'undefined') {// IE
				var xml = new ActiveXObject("Microsoft.XMLDOM");
				xml.async = false;
				xml.load(XML);
			}
			else {	// others
				var myXMLHTTPRequest = new XMLHttpRequest();
				myXMLHTTPRequest.open("GET", XML, false);
				myXMLHTTPRequest.send(null);
				var xml = myXMLHTTPRequest.responseXML;
			}
			return xml;
		}
		
		function getDomFromXml(xml) {

			if (typeof ActiveXObject != 'undefined') {
				var dom = new ActiveXObject("Microsoft.XMLDOM");
				dom.async = false;
				dom.loadXML(xml);
			}
			else {
				parser = new DOMParser();
				dom = parser.parseFromString(xml, "text/xml");						
			}
			return dom;
		}


		function xslt(xmlDoc,xslDoc) {
			var transform;
			
			if (typeof ActiveXObject != 'undefined') {
				transform = xmlDoc.transformNode(xslDoc);
			}
			else {
				var xsl = new XSLTProcessor();
				
				xsl.importStylesheet(xslDoc);
				var fragment=xsl.transformToFragment(xmlDoc, document);
				if( fragment.childNodes.length>0 )
				  transform = fragment.childNodes[0].innerHTML;
				else
					alert("error");
			}
			return transform;
		}