PDA

View Full Version : loading XML...


bphein1980
09-27-2005, 01:05 AM
I have this script to load an xml doc through a link...

If you look at the last line of the Mozilla part, it uses .appendChild, which of course just adds each xml link into the document. It does not replace the previous loaded xml doc.

I tried using replaceChild, but it will not work in Firefox. I can only get this script to work properly through IE.

Can anyone fix this work in both IE and Firefox. That is that it will replace the xml document instead of adding them together as links are clicked?

Many thanks!

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

HTML

<a href="javascript:transform('whatever1.xml')">1</a>
<a href="javascript:transform('whatever2.xml')">2</a>
<a href="javascript:transform('whatever3.xml')">3</a>

Alex Vincent
09-27-2005, 05:55 AM
XMLHttpRequest is asynchronous. You need to split your function after the send() call, and put the second half in an onload event listener for XMLHttpRequest.

bphein1980
09-27-2005, 07:27 PM
Thanks for the reply. I have not a clue on how to do that.

This actually something that I have taken over for a co worker. To say the least, it is not my strongpoint.

Any examples that you can link me to or write?

Thanks!

bphein1980
09-28-2005, 08:41 AM
I have tried to fix this, but all my attempts fail.

Is there anyone that can fix this script to work correctly in both Firefox and IE.

I need for the xml data to be replaced, not added.

replaceChild does not work in FF.

Anyone?

bphein1980
10-02-2005, 01:42 AM
Found the Solution...

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