By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,384 Members | 1,794 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,384 IT Pros & Developers. It's quick & easy.

XSLT Transformation in Javascript

P: n/a
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.

Aug 29 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a


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/default.asp?url=/library/en-us/xmlsdk/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/default.asp?url=/library/en-us/xmlsdk/html/f7aaa1b1-426e-4f5c-9df9-b921215815fc.asp>
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/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.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 30 '06 #2

P: n/a
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?

Aug 30 '06 #3

P: n/a
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?

Aug 30 '06 #4

P: n/a


Tom Cole wrote:
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.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 30 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.