Skip to content Skip to sidebar Skip to footer

Uncaught Typeerror: Failed To Execute 'importstylesheet' On 'xsltprocessor': Parameter 1 Is Not Of Type 'node'

I am trying to use XSL to translate an XML file into a neat table. For that I used the examples provided by W3schools which can be located here as a starting point. Yet the browser

Solution 1:

Here is an example of two asynchronous requests where the callback of one event handler starts the next request whose callback does the transformation. To keep it simple, I have used onload instead of onreadystatechange, if you really need support for old IE versions you will need to adapt the code.

<!DOCTYPE html><html><head><metacharset="UTF-8"><title>XMLHttpRequest and onload handler with asynchronous requests</title><script>functionload(url, callback) {
  var req = newXMLHttpRequest();
  req.open('GET', url);
  // to allow us doing XSLT in IEtry { req.responseType = "msxml-document" } catch (ex) {}
  req.onload = function() {
    callback(req.responseXML);
  };
  req.send();
}

functiontransform(xml, xsl) {
  load(
    xml,
    function(inputXml) {
      load(
        xsl,
        function(xsltSheet) {
          displayResult(inputXml, xsltSheet);
        }
      );
    }
  );
}

functiondisplayResult(xmlInput, xsltSheet) {
  if (typeofXSLTProcessor !== 'undefined') {
    var proc = newXSLTProcessor();
    proc.importStylesheet(xsltSheet);
    document.getElementById('example').appendChild(proc.transformToFragment(xmlInput, document));
  }
  elseif (typeof xmlInput.transformNode !== 'undefined') {
    document.getElementById("example").innerHTML = xmlInput.transformNode(xsltSheet);
  }
}
</script></head><bodyonload="transform('catalog.xml', 'catalog.xsl')"><divid="example"></div></body></html>

Online at http://home.arcor.de/martin.honnen/xslt/test2015072001.html, works fine with current versions of IE, Firefox and Chrome on Windows 8.1.

If you want to start two asynchronous requests directly to load XML and XSLT then you need to do some more work to make sure you know when both documents have been loaded to process them, an example of that is at http://home.arcor.de/martin.honnen/xslt/test2015072101.html:

<!DOCTYPE html><html><head><metacharset="UTF-8"><title>XMLHttpRequest and onload handler with asynchronous requests</title><script>functionmakeRequest(url, loadedData, property, elementToAddResult) {
  var req = newXMLHttpRequest();
  req.open('GET', url);
  // to allow us doing XSLT in IEtry { req.responseType = "msxml-document" } catch (ex) {}
  req.onload = function() {
    loadedData[property] = req.responseXML;
    if (checkLoaded(loadedData)) {
      displayResult(loadedData.xmlInput, loadedData.xsltSheet, elementToAddResult);
    };
  };
  req.send();
}  

functioncheckLoaded(loadedData) {
  return loadedData.xmlInput != null && loadedData.xsltSheet != null;
}

functionloadAndTransform(xml, xsl, elementToAddResult) {
  var loadedData = { xmlInput: null, xsltSheet: null };

  makeRequest(xml, loadedData, 'xmlInput', elementToAddResult);
  makeRequest(xsl, loadedData, 'xsltSheet', elementToAddResult);
}  

functiondisplayResult(xmlInput, xsltSheet, elementToAddResult) {
  if (typeofXSLTProcessor !== 'undefined') {
    var proc = newXSLTProcessor();
    proc.importStylesheet(xsltSheet);
    elementToAddResult.appendChild(proc.transformToFragment(xmlInput, document));
  }
  elseif (typeof xmlInput.transformNode !== 'undefined') {
    elementToAddResult.innerHTML = xmlInput.transformNode(xsltSheet);
  }
}
</script></head><bodyonload="loadAndTransform('catalog.xml', 'catalog.xsl', document.getElementById('example'));"><divid="example"></div></body></html>

Post a Comment for "Uncaught Typeerror: Failed To Execute 'importstylesheet' On 'xsltprocessor': Parameter 1 Is Not Of Type 'node'"