473,411 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

XSLT Transformation in Javascript

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


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


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sergio del Amo | last post by:
Hi, I use the xslt functions provided by php. I am running in my computer the package xampp(www.apachefriends.org) which includes php/apache/mysql .. In this package the php includes the sablotron...
6
by: Pete | last post by:
I am just getting to grips with XML and I was wondering if you could help me with something that no-one seems able or willing to help with.. I have an XSLT file which should be transforming a...
2
by: Jon Martin Solaas | last post by:
For java programmers there exist a framework called Millstone (www.millstone.org) for programming web user-interfaces in a component oriented way. Millstone uses xslt transformations to render...
8
by: David Dorward | last post by:
I'm looking for an XSLT that I can use to transform XHTML 1.0 Strict into HTML 4.01. Does anyone know of a nice prewritten one? -- David Dorward ...
7
by: RC | last post by:
First, let me say I couldn't find a group discuss XML/XSLT. So I only choose the closest groups to post this message. Here is part of my *.xsl file <xsl:stylesheet...
4
by: Stephen | last post by:
I have the following that outputs an xml file to a div using ajax: <script type="text/javascript"> function ajaxXML(url,control_id){ if (document.getElementById) { var x =...
5
by: shauldar | last post by:
Is there a way (tool, hack...) to create an XSL:FO from an XSLT + XML files? My motivation is that we want to use a tool to design reports, and from that "design" generate both HTML (via XSLT)...
2
by: RJN | last post by:
Hi Can XSLT be applied for transforming .txt files or are they applicable only for xml to xml transformation. Currently all the clients send file to a server in one particular format. The...
4
by: Philipp Reiss | last post by:
Hello group, I'm new in this topic and I run into problems where google can't help me. I have a XML-file, wich is bound to a XSLT-file, and this works fine in a browser. How do I show the same...
3
by: RC | last post by:
Let's say: if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) { // Now I got an XML object here var xmlDocument = XMLHttpRequestObject.responseXML; // next I have...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.