...

View Full Version : JS load XML/XSL - works IE not FF



bphein1980
09-26-2005, 07:33 PM
I have this script:


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!

KC-Luck
09-26-2005, 07:53 PM
look here:
Mozilla:XML (http://www.mozilla.org/xmlextras/)
Mozilla:XSLT (http://www.mozilla.org/projects/xslt/)

bphein1980
09-26-2005, 09:03 PM
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!

KC-Luck
09-26-2005, 09:59 PM
some more reading: (i hate reading!) :(
XSLTProcessor (http://www.xulplanet.com/references/objref/XSLTProcessor.html)
Sarissa (cross-browser XML/XSL Library) (http://sarissa.sourceforge.net/doc/XSLTProcessor.html)

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

bphein1980
09-26-2005, 10:22 PM
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:

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");
}

}

bphein1980
09-28-2005, 07:39 AM
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?

bphein1980
10-01-2005, 12:49 AM
One more bump. Can anyone help me on this?

bphein1980
10-02-2005, 12:41 AM
Found the solution to this problem....




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");
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum