XSLT Transformation in Javascript

T

Tom Cole

I am trying to create a cross-browser implementation that will take an
XML file, apply a XSL stylesheet to it and return a DOM stack of the
styled XML.

In non-IE browsers I am using something like the following
successfully:

NOTE: value is a global variable and element & url are passed to the
method. Element is the document element that is to contain the parsed
XML DOM and url points to the XSL stylesheet.

if (typeof XSLTProcessor != 'undefined') {
var processor = new XSLTProcessor();
var request = getXMLHttpRequest();
if (request) {
request.open("GET", url, false);
request.send(null);
var xsl = request.responseXML;
processor.importStylesheet(xsl);
var fragment = processor.transformToFragment(value, document);
if (element)
element.appendChild(fragment);
}
else {
return;
}

}

In IE there is no XSLTProcessor, but there is the XMLDOM ActiveX
component. Problem is I don't know how to get it to return me an XML
DOM, just text:

else {
var processor = new ActiveXObject("Microsoft.XMLDOM");
if (processor) {
processor.async = false;
processor.load(url);
var xmlString = value.transformNode(processor);
if (element) {
element.innerHTML = xmlString;
}
else {
return;
}

}

I've only found the transformNode method which returns a String, but
nothing that will return actual DOM objects.

Thanks in advance.
 
M

Martin Honnen

Tom Cole wrote:

In IE there is no XSLTProcessor, but there is the XMLDOM ActiveX
component. Problem is I don't know how to get it to return me an XML
DOM, just text:

else {
var processor = new ActiveXObject("Microsoft.XMLDOM");
if (processor) {
processor.async = false;
processor.load(url);
var xmlString = value.transformNode(processor);

IE uses MSXML for transformations, if you want to transform to a new XML
DOM document then create one e.g.
var resultDocument = new ActiveXObject('Msxml2.DOMDocument.3.0');
value.transformNodeToObject(stylesheetDocument, resultDocument);
<http://msdn.microsoft.com/library/d...html/ece045e3-85b3-46ed-85b4-8532f076ea79.asp>


Alternatively you can also use XSLTemplate to create a processor object,
check the MSXML documentation for details
<http://msdn.microsoft.com/library/d...html/f7aaa1b1-426e-4f5c-9df9-b921215815fc.asp>
<http://msdn.microsoft.com/library/d...html/af1fe9e0-b3f4-4ea1-8b6c-ab25a619be59.asp>
If you want an XML DOM result document with that approach as well then
you need to create the result document as above and assign it to the
output property of the IXSLProcessor object before you call the
transform method.
 
T

Tom Cole

Well here's what I have now that apparently gives me an object, however
it is not one that I can append to a document element.

Some additional background:

The value variable contains the results of XMLHttpRequest.responseXML.
The XSL stylesheet transforms the results into an HTML table. I then
want to append the table to the reference document element.

My code now looks like:

var stylesheet = new ActiveXObject("Msxml2.DOMDocument.3.0");
if (stylesheet) {
stylesheet.async = false;
stylesheet.load(url);
var results = new ActiveXObject("Msxml2.DOMDocument.3.0");
results.async = false;
results.validateOnParse = true;
value.transformNodeToObject(stylesheet, results);
if (element) //div element in the document passed as an argument...
element.appendChild(results);
}

The stylesheet loads and apparently the results contains something as
an alert shows [object]. However I cannot append results to the
element. I get an error that says No such interface supported.

Do I need to do something to convert the value object into an XMLDOM
object first?
 
T

Tom Cole

Actually a tried replacing:

element.appendChild(results);

with

element.appendChild(results.documentElement);

but still no luck. The good news is the transformation went okay.
results.documentElement.xml has the correctly transformed XML in it.

Any ideas why appendChild isn't working for this?
 
M

Martin Honnen

Tom said:
Well here's what I have now that apparently gives me an object, however
it is not one that I can append to a document element.

See answer in microsoft.public.scripting.jscript.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top