Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Posts
    136
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JS load XML/XSL - works IE not FF

    I have this script:

    Code:
          var ss, cache;
    
          function init()
          {
            // create and load XSLT stylesheet – must be free-threaded
            ss = new ActiveXObject('MSXML2.FreeThreadedDOMDocument.4.0');
            ss.async = false;
            ss.load('stylesheet.xsl');
    
            // create XSLTemplate object and compile stylesheet into it
            cache = new ActiveXObject("Msxml2.XSLTemplate.4.0");
            cache.stylesheet = ss;
          }
    
          function transform(source)
          {
            // create and load source document
            var src = new ActiveXObject('MSXML2.DOMDocument.4.0');
            src.async = false;
            src.load(source);
    
            // retrieve cached XSLProcessor and transform the source document
            var proc = cache.createProcessor();
            proc.input = src;
            proc.transform();
            target.innerHTML = proc.output;
          }
    It is called like so in the HTML page:
    <a href="javascript:transform('whatever.xml')">

    It works just fine in IE, but not in Firefox. Can anyone tell me how to get this to work in Firefox? Or would it not be supported at all?

    Thanks!

  • #2
    Regular Coder
    Join Date
    Aug 2005
    Posts
    282
    Thanks
    0
    Thanked 0 Times in 0 Posts

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Posts
    136
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for the reply KC-Luck.

    I am still unsure of how to get it to work on Firefox. I am very new to javascript and xml/xsl. The script is actually an example I found on the web.

    Any further help making the script work on both IE and Firefox would be helpful.

    Thank you!

  • #4
    Regular Coder
    Join Date
    Aug 2005
    Posts
    282
    Thanks
    0
    Thanked 0 Times in 0 Posts
    some more reading: (i hate reading!)
    XSLTProcessor
    Sarissa (cross-browser XML/XSL Library)

    I don't use sarissa, but you may get some pointers from it...

  • #5
    Regular Coder
    Join Date
    Feb 2005
    Posts
    136
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here we go.

    It works in FF and IE, but it is not replacing the XML data... it is just adding it on at the end.

    I believe it has to do with the part ".appendChild"

    Can someone take a look at the code below and tell me how to get it to replace the xml (source), instead of adding it at the end?

    replaceChild works in IE, but not in FF. Is there a crossbrowser option?

    Thank you

    CODE:
    Code:
    function transform(source)
    	{
    			if(document.implementation && document.implementation.createDocument)
    			{
    				// Mozilla
    		
    				var xsltProcessor = new XSLTProcessor();
    				
    				// load the xslt file
    				var myXMLHTTPRequest = new XMLHttpRequest();
    				myXMLHTTPRequest.open("GET", "stylesheet.xsl", false);
    				myXMLHTTPRequest.send(null);
    				
    				// get the XML document
    				xslStylesheet = myXMLHTTPRequest.responseXML;
    				xsltProcessor.importStylesheet(xslStylesheet);
    				
    				// load the xml file
    				myXMLHTTPRequest = new XMLHttpRequest();
    				myXMLHTTPRequest.open("GET", source, false);
    				myXMLHTTPRequest.send(null);
    				
    				var xmlSource = myXMLHTTPRequest.responseXML;
    				
    				//transform
    				 var resultDocument = xsltProcessor.transformToFragment(xmlSource, document);
    				document.getElementById("target").appendChild(resultDocument);
    				
    			}else if(window.ActiveXObject){
    				// IE
    				
    				// Load XML
    				xml = new ActiveXObject("MSXML2.DOMDocument");
    				xml.async = false
    				xml.load(source)
    				
    				// Load XSL
    				xsl = new ActiveXObject("MSXML2.DOMDocument");
    				xsl.async = false
    				xsl.load("stylesheet.xsl")
    				
    			
    				// Transform
    				document.getElementById("target").innerHTML=xml.transformNode(xsl);
    			}else{
    				// Browser unknown
    				alert("Browser unknown");
    			}
    		
    		}
    Last edited by bphein1980; 09-26-2005 at 10:27 PM.

  • #6
    Regular Coder
    Join Date
    Feb 2005
    Posts
    136
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Anyone?

    I cannot get this script to work correctly in Firefox. I need it to replace the xml data, not append to it.

    replaceChild does not work either.

    Is there anyone that can fix this?

  • #7
    Regular Coder
    Join Date
    Feb 2005
    Posts
    136
    Thanks
    0
    Thanked 0 Times in 0 Posts
    One more bump. Can anyone help me on this?

  • #8
    Regular Coder
    Join Date
    Feb 2005
    Posts
    136
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Solution

    Found the solution to this problem....



    Code:
    var xslStylesheet;
    var xsltProcessor = new XSLTProcessor();
    var myDOM;
    
    var xmlDoc;
    
    function transform(source,stylesheet)
    {
    	if(document.implementation && document.implementation.createDocument)
    	{
    
      		// load the xslt file
      		var myXMLHTTPRequest = new XMLHttpRequest();
     		myXMLHTTPRequest.open("GET", stylesheet, false);
      		myXMLHTTPRequest.send(null);
    
      		xslStylesheet = myXMLHTTPRequest.responseXML;
      		xsltProcessor.importStylesheet(xslStylesheet);
    
      		// load the xml file
      		myXMLHTTPRequest = new XMLHttpRequest();
      		myXMLHTTPRequest.open("GET", source, false);
      		myXMLHTTPRequest.send(null);
    
      		xmlDoc = myXMLHTTPRequest.responseXML;
    
      		var fragment = xsltProcessor.transformToFragment(xmlDoc, document);
    
      		document.getElementById("target").innerHTML = "";
    
      		myDOM = fragment;
      		document.getElementById("target").appendChild(fragment);
    	}
    	else if(window.ActiveXObject)
    	{
    		// IE
    			
    		// Load XML
    		xml = new ActiveXObject("MSXML2.DOMDocument");
    		xml.async = false
    		xml.load(source)
    			
    		// Load XSL
    		xsl = new ActiveXObject("MSXML2.DOMDocument");
    		xsl.async = false
    		xsl.load(stylesheet)
    
    		// Transform
    		document.getElementById("target").innerHTML=xml.transformNode(xsl);
    	}
    	else
    	{
    		// Browser unknown
    		alert("Browser unknown");
    	}
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •