Re: XSL file load
"Seamus" <[email protected]>
| Newsgroups | gmane.comp.mozilla.devel.layout.xslt |
|---|---|
| Organization | Another Netscape Collabra Server User |
| Message-ID | <[email protected]> |
First thanks for the timely responce. I tried your suggestions but to no
avail...
I am attaching my entire script....the
alert("xmlDoc >> \n" + xmlDoc.xml);
displays correctly....however the XSL displays nothing....
I have tried loading Asynch and Synch and have had no luck...Sarissa doesn't
seem to do anything special..so I am not sure where I am goofing....the rel
path to the file must be OK as IE has no problem finding it.
----------------------------------------
SCRIPT
-----------------------------------------
//Enumerated interface of Browser types (pull into new interface)
var BrowserType_Undefined = 0;
var BrowserType_Mozilla = 1;
var BrowserType_IE = 2;
//
//Members
//var parser;
//var domDoc;
var browserType = 0;
var processor = new XSLTProcessor();
function setAlert(alertData){
// parser = new DOMImplementation();
// parser.preserveWhiteSpace = true;
//
// try {
// domDoc = parser.loadXML(alertData);
// } catch(e) {
// alert(e.name + " " + e.description + " " + e.message)
// }
//
// //filter baseed on XPath
// var xpathExpression = '/rootnode';
// var xpathNodeSet = domDoc.selectNodeSet(xpathExpression);
//validate browser and set global info (browser type)
//transform the DOM
var xslSource = "script/xslt/alertsSummary-rtam.xslt";
var transformedAlerts = XSLtransform(alertData, xslSource);
//write alerts to summary target area
alert("Final > " + transformedAlerts);
top.main.document.getElementById("alertSummary").innerHTML =
transformedAlerts;
// htmlizedxml = htmlizeit(domDoc.toString());
// document.getElementById("divxml").innerHTML = htmlizedxml;
} // end function init()
function XSLtransform(xmlSource, xslSource) {
//validate browser
if((typeof document.implementation != 'undefined') &&
(typeof document.implementation.createDocument != 'undefined'))
{
browserType = BrowserType_Mozilla;
} else if(typeof window.ActiveXObject != 'undefined') {
browserType = BrowserType_IE;
}
// alert(xmlSource);
var transformedDOM;
switch (browserType) {
case BrowserType_IE :
alert("IE");
//IE Transform Syntax
var xml = new ActiveXObject("MSXML2.DOMDocument");
//xml.validateOnParse = false;
xml.async = false;
xml.loadXML(xmlSource);
alert(xml.xml);
if (xml.parseError.errorCode != 0) {
alert("XML Parse Error : " + xml.parseError.reason);
}
var xsl = new ActiveXObject("MSXML2.FreeThreadedDOMDocument");
xsl.async = false;
xsl.load(xslSource);
if (xsl.parseError.errorCode != 0) {
alert("XSL Parse Error : " + xsl.parseError.reason);
}
try {
var templ = new ActiveXObject("MSXML2.XSLTemplate");
templ.stylesheet = xsl;
var xslp = templ.createProcessor();
xslp.input = xml;
xslp.transform();
transformedDOM = xslp.output;
} catch (e) {
alert('error: setup xslt: ' + e.description + '\n' +
msgExplain);
}
break;
case BrowserType_Mozilla :
alert("MOZILLLLLLLA!!!!!");
//setup XML DOC
// var myXMLHTTPRequest = new XMLHttpRequest(); // this works the same,
// myXMLHTTPRequest.open("GET", 'test.xml'); // apparently
// myXMLHTTPRequest.send(null); //
// var xmlDoc = myXMLHTTPRequest.responseXML; //
// alert(xmlDoc.documentElement.tagName);
//override LOAD so that we can set readyState
Document.prototype.__load__ = Document.prototype.load;
Document.prototype.load = _Document_load;
Document.prototype.onreadystatechange = null;
Document.prototype.parseError = 0;
Node.prototype.__defineGetter__("xml", _Node_getXML);
//add the loadXML() method to the Document class
Document.prototype.loadXML = function(strXML) {
//create new document from string
var objDoc = new DOMParser().parseFromString(strXML,
"text/xml");
copyChildNodes(objDoc, this);
//we can't fire the onload event, so we fake it
handleOnLoad(this);
}
try {
var xmlDoc = document.implementation.createDocument("", "",
null);
xmlDoc.async = false;
xmlDoc.loadXML(xmlSource);
xmlDoc.addEventListener("load", _Document_onload, false);
} catch (e) {
alert('error: load xml: ' + e );
}
alert("xmlDoc >> \n" + xmlDoc.xml);
var xslStr = "<?xml version='1.0' encoding='UTF-8'?>"+
"<xsl:stylesheet version='1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform' >"+
"<xsl:output method='html' version='1.0' encoding='UTF-8'
indent='yes'/>"+
//"<xsl:param name='title'><xsl:value-of select=\"'default
title'\"/></xsl:param>" +
"<xsl:template match='/rootnode'>" +
" <html>" +
" <head>" +
" <title>Test</title>" +
" </head>" +
" <body>" +
" <h2>TEST</h2>" +
" <xsl:apply-templates select='//alert' />" +
" </body>" +
" </html>" +
" </xsl:template>" +
" <xsl:template match='alert'>" +
" <div class='alert'>" +
" <xsl:value-of select='title'/>" +
" <xsl:value-of select='timestamp'/>" +
" </div>" +
" </xsl:template> " +
"</xsl:stylesheet>";
//+" </xsl:template>
// "<xsl:template match='/'><p class='test' title='{$title}'>test</p>"+
// "</xsl:template><xsl:template match='@*'>"+
// "</xsl:template>
//set up the XSL DOC and transformer
var xslDoc = document.implementation.createDocument("", "",
null);
xslDoc.async = false;
// xslDoc.loadXML(xslStr);
xslDoc.addEventListener("load", function(evt)
{_Document_onload_XSLT(xslDoc);}, false);
xslDoc.load(xslSource);
alert("xslDoc >> \n" + xslDoc.xml);
transformedDOM = processor.transformToDocument(xmlDoc).xml;
alert("transformedDOM >> \n" + transformedDOM );
// transformedDOM = new
XMLSerializer().serializeToString(transformedDOM.);
// transformedDOM =
transformedDOM.getElementsByTagName("rootnode");
break;
default :
alert("unsupported browser type");
}
return transformedDOM;
// function onload() {
// processor.importStylesheet(xslDoc);
// }
}
function _Node_getXML() {
//create a new XMLSerializer
var objXMLSerializer = new XMLSerializer;
//get the XML string
var strXML = objXMLSerializer.serializeToString(this);
//return the XML string
return strXML;
}
function _Document_load(strURL) {
//set the parseError to 0
this.parseError = 0;
//change the readyState
changeReadyState(this, 1);
try {
//call the original load method
this.__load__(strURL);
} catch (objException) {
//set the parseError attribute
this.parseError = -9999999;
//set readyState to 4, we are done loading
changeReadyState(this, 4);
}
}
function _Document_onload() {
//change the readyState
changeReadyState(this, 4);
}
function _Document_onload_XSLT(xslDoc) {
processor.importStylesheet(xslDoc);
//change the readyState
changeReadyState(this, 4);
}
function changeReadyState(objDOMDocument, iReadyState) {
//change the readyState
objDOMDocument.readyState = iReadyState;
//if there is an onreadystatechange event handler, run it
if (objDOMDocument.onreadystatechange != null && typeof
objDOMDocument.onreadystatechange == "function")
objDOMDocument.onreadystatechange();
}
function handleOnLoad(objDOMDocument) {
//check for a parsing error
if (!objDOMDocument.documentElement ||
objDOMDocument.documentElement.tagName == "parsererror")
objDOMDocument.parseError = -9999999;
//change the readyState
changeReadyState(objDOMDocument, 4);
}
/**
* <p>Deletes all child nodes of the given node</p>
* @argument startNode the Node to empty
*/
function deleteChildNodes(startNode) {
while (startNode.hasChildNodes()) {
startNode.removeChild(startNode.lastChild);
}
}
/**
* <p> Replaces the childNodes of the toDoc object with the childNodes of
* the fromDoc object</p>
* <p> <b>Note:</b> The second object's original content is deleted before
the copy operation</p>
* @argument nodeFrom the Node to copy the childNodes from
* @argument nodeTo the Node to copy the childNodes to
*/
function copyChildNodes(nodeFrom, nodeTo) {
deleteChildNodes(nodeTo);
var ownerDoc = nodeTo.nodeType == Node.DOCUMENT_NODE ? nodeTo :
nodeTo.ownerDocument;
var nodes = nodeFrom.childNodes;
if(ownerDoc.importNode){
for(var i=0;i < nodes.length;i++) {
nodeTo.appendChild(ownerDoc.importNode(nodes[i], true));
};
}
else{
for(var i=0;i < nodes.length;i++) {
nodeTo.appendChild(nodes[i].cloneNode(true));
};
};
};
"Martin Honnen" <[email protected]> wrote in message
news:[email protected]...
>
>
> Seamus wrote:
>
>> I am having issues loading an XSL file into my script (on FireFox). The
>> stylesheet is below. I am trying to load via the syntax below. If I try
>> to load via the loadXML extension that I wrote, all works well. If I try
>> to load from the file...I get nothing in the resulting document.
>
>> var xslDoc = document.implementation.createDocument("", "",
>> null);
>> // xslDoc.async = false;
>> // xslDoc.loadXML(xslStr);
>> xslDoc.load(xslSource);
>> xslDoc.addEventListener("load",
>> _Document_onload_XSLT(xslDoc), false);
>
> You should add the event listener before you call load I think and of
> course you need to make sure the event listener argument is a function and
> not a function call so try
> xslDoc.addEventListener(
> "load",
> function (evt) {
> _Document_onload_XSLT(xslDoc);
> },
> false
> );
> xslDoc.load(xslSource);
>
>
>
> --
>
> Martin Honnen
> http://JavaScript.FAQTs.com/