0

I am trying to get a XML document sorted and decided to go for the "sort via XSLT" approach.

However, I am having trouble updating my two global variables that should contain the content of the XML and XSLT files and I can't really figure out why.

Up until now I never had this kind of problem and global variables used to work... I also didn't declare them inside the functions, but used the global name instead and also tried using window.variable, but to no avail.

Does anyone have an idea why the code doesn't update the global variable?

best regards,

daZza

<script type="text/javascript"> 

var xml = "";
var xsl = "";


function callSort()
{
    loadSortXML();
    loadSortXSLT();
    sortXML();
}


function loadSortXML()
{
    var xmlHttp = null;
    var xmlData;
    var xmlFile = "data/LessonsLearned.xml";

    if (typeof XMLHttpRequest != 'undefined')
    {
        xmlHttp = new XMLHttpRequest();
    }

    if (!xmlHttp)
    {
        try
        {
            xmlHttp = new ActiveXObject("Msxm12.XMLHTTP");
        } 
        catch(e)
        {
            try
            {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")
            } 
            catch(e)
            {
                xmlHttp = null;
            }
        }
    }

    if (xmlHttp)
    {
        var url = xmlFile;
        xmlHttp.open("GET", url, true);
        xmlHttp.onreadystatechange = function() 
        {
            if (xmlHttp.readyState == 4)
            {               
                xml = xmlHttp.responseXML;
            }
        }
        xmlHttp.send();
    }
}


function loadSortXSLT()
{
    var xmlHttp = null;
    var xmlData;
    var xmlFile = "data/xslt.xml";

    if (typeof XMLHttpRequest != 'undefined')
    {
        xmlHttp = new XMLHttpRequest();
    }

    if (!xmlHttp)
    {
        try
        {
            xmlHttp = new ActiveXObject("Msxm12.XMLHTTP");
        } 
        catch(e)
        {
            try
            {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")
            } 
            catch(e)
            {
                xmlHttp = null;
            }
        }
    }

    if (xmlHttp)
    {
        var url = xmlFile;
        xmlHttp.open("GET", url, true);
        xmlHttp.onreadystatechange = function() 
        {
            if (xmlHttp.readyState == 4)
            {                       
                xsl = xmlHttp.responseXML;
            }
        }
        xmlHttp.send();
    }
}


function sortXML()
{
    console.log("XML " + xml);
    console.log("XSL "+ xsl);

    var parser = new DOMParser();
    var domToBeTransformed = parser.parseFromString(xml, "text/xml");
    var xslt = parser.parseFromString(xsl, "text/xml");
    var processor = new XSLTProcessor();

    processor.importStylesheet(xslt);

    var newDocument = processor.transformToDocument(domToBeTransformed);            
    var serializer = new XMLSerializer();
    var newDocumentXml = serializer.serializeToString(newDocument);
    alert(newDocumentXml);
}

</script>   
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
daZza
  • 1,669
  • 1
  • 29
  • 51
  • 1
    It does update the variables, but not when you think it does. AJAX is asynchronous, so the variables are not updated until the response is received and the `onreadystatechange` handler runs. You're calling `sortXML` immediately, not after the responses are received. – Barmar May 08 '14 at 08:34
  • possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Barmar May 08 '14 at 08:35
  • Cheers @Barmar, I forgot to keep that in mind :/ Everything works fine now in that regard. – daZza May 08 '14 at 08:38

0 Answers0