Riddle me this: XMLHttpRequest vs. createDocument

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Riddle me this: XMLHttpRequest vs. createDocument

tswaters
I've searched far and wide for ways to load an XML document using Mozilla, I've come up with two:  
 - XMLHttpRequest, connect to server & using the responseXML to load XML Document
 - createDocument and using a load call to load an XML Document.  

Below, I've got code below for the both of 'em in the same JS script, separated by a simple prompt asking for a "YES" to make it work, and "NO" (or anything else) to make it not work...  
 - YES calls the XMLHttpRequest code,  
 - NO calls that createDocument code.

Can someone tell me what I'm doing wrong with the createDocument code?  Instead of appending the XMLDocument to the example div, as expected, it instead does nothing.... I'm not even getting any errors.

example.xml: example.xml
example.xsl: example.xsl

example.htm (HTML):
<html>
<head>
[ insert script here -- see below ]
</head>
<body onLoad="Init();">
<div id=example></div>
</body>
</html>

example.htm  (javascript)

function Init() {
  // note, excluding code for IE -- this is a mozilla forum!
  if (document.implementation && document.implementation.createDocument) {

        if ( prompt('type "yes" to work, "no" to not').toUpperCase() == "YES" ) {

                // load the xsl file, example.xsl
                var myXMLHTTPRequest = new XMLHttpRequest();
                myXMLHTTPRequest.open("GET", "example.xsl", false);
                myXMLHTTPRequest.send(null);
                xslStylesheet = myXMLHTTPRequest.responseXML;

                // load the xml file, example.xml
                myXMLHTTPRequest = new XMLHttpRequest();
                myXMLHTTPRequest.open("GET", "example.xml", false);
                myXMLHTTPRequest.send(null);
                xmlSource = myXMLHTTPRequest.responseXML;

        } else {

                // load the xsl file, example1.xsl
                var xslStylesheet = document.implementation.createDocument("", "", null);
                xslStylesheet.load( "example.xsl" );

                // load the xml file, example1.xml
                var xmlSource = document.implementation.createDocument("", "", null);
                xmlSource.load("example.xml");
        }

        var xsltProcessor = new XSLTProcessor();
        xsltProcessor.importStylesheet(xslStylesheet);

        var resultDocument = xsltProcessor.transformToFragment(xmlSource, document);
        document.getElementById('example').appendChild(resultDocument);
  }
}

Any help = greatly appreciated.   I can probably just use XMLHttpRequest -- but I'd like to at least know why the second method isn't working, if I'm missing something... or anything...  thanks again

-Tyler Waters