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>
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>