PDA

View Full Version : Keeping an object instatiated while loading XML documents



yea420
Mar 27th, 2007, 05:18 PM
Hi Everyone, and thanks in advance for any help.

I'm feeling a little stuck with this one. I'm trying to create an XSL object in my namespace that will handle any client side transformations. I'm having difficulties.

I'm pretty sure that it's due to the XML object takeing longer to load than the executing thread, ergo at some point I lose my object. Here is the code:




var SCT = (function() {

//****************** XSL Transform object *********************
//
// Transforms XML documents with an XSL
//*******************************************************************

function Xsl() {
// My instance of an xslt transform object
this.xmlDoc;
this.xslDoc;
this.strXmlDoc = "";
this.strXslDoc = "";
this.output = "";
}
Xsl.prototype.urlTransform = function(strXDoc, strXSDoc) {
this.strXmlDoc = strXDoc;
this.strXslDoc = strXSDoc;
// Load our xmlDocuments
alert(this.strXmlDoc);
this.urlLoad(this.strXmlDoc, this.urlLoadXml);
}

Xsl.prototype.urlLoadXml = function (xDoc) {
alert('XML Loaded');
this.xmlDoc = xDoc;
this.urlLoad(this.strXslDoc, this.urlLoadXsl);
}
Xsl.prototype.urlLoadXsl = function (xDoc) {
// set our xmlDocument
alert('XSL Loaded');
this.xslDoc = xDoc;
this.doTransform();
}
Xsl.prototype.urlLoad = function(url, handler) {
// Determine which category of browser we're looking at
if (document.implementation && document.implementation.createDocument) {
// Gecko
var objXml = document.implementation.createDocument("", "", null);
objXml.onload = function () { handler(objXml, url); }
objXml.load(url);
while(!objXml.onload){}

} else if(window.ActiveXObject) {
// Windows
var objXml = new ActiveXObject("Microsoft.XMLDOM");
objXml.onreadystatechange = function() {
if(objXml.readyState == 4) handler(objXml);
}
objXml.load(url);
}
}
Xsl.prototype.doTransform = function() {
alert('Transforming');
// The final call, transforms the Xml and Xsl document into our output
this.output = '';
if (window.XSLTProcessor) {
// support for Gecko based browsers
var xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(this.xslDoc);
this.output = xsltProcessor.transformToFragment(this.xmlDoc, document);
} else if(window.ActiveXObject) {
// support for Windows/ActiveX enabled browsers
this.output = this.xmlDoc.transformNode(this.xslDoc);
} else {
this.output = "Your browser does not support client XSLT Transformations";
}
}


// Return our Objects as properties
return {
Xsl : Xsl
}
})();



To call it I use:




var sctXSL = new SCT.Xsl();
sctXSL.urlTransform(strXML, strXSL);
alert(sctXSL.output);



I get the alert saying "XML Loaded" but then an error "this.urlLoad is not a function". Like I said above, I assume that this is becuase the executing thread has left the building, but I'm not exactly sure how to fix this.

Much thanks,

Spencer