views:

65

answers:

1

I have some JavaScript that needs to apply an xslt to the result of an ajax request. The xslt is defined in a separate file "transform.xslt" - whats the best way of including the xslt in my page?

Is it possible to use some sort of <link... element, or will I need to make a separate http request myself for the xslt?

+2  A: 

I think you need to use XMLHttpRequest to load the stylesheet from the server, then you have the responseXML and you can use that with the XSLT transformation APIs the browsers expose to Javascript (e.g. importStylesheet with Mozilla and transformNode with IE).

Unless you target IE only where you could use an XML data island with e.g.

<xml id="sheet1" src="sheet.xslt></xml>

in the head section of your HTML document. Then your script could use e.g. someResponseXML.transformNode(sheet1.XMLDocument).

Martin Honnen
So you don't think that the browser would implicitly perform the transform if the XML had the right header? Of course you're right in the case that the XML was just plain XML with no stylesheet reference.
Pointy
The original question says "apply an xslt to the result of an ajax request". In my understanding that means script has received some responseXML object and now wants to apply an XSLT stylesheet. To do that you have to load the stylesheet first with script. Of course if you do not load the original XML with script and XMLHttpRequest but instead load it in a window or frame then you can expect the browser to apply any stylesheet referenced in an xml-stylesheet processing instruction. But that is only happening if the XML is loaded in a frame/window, not if it has been loaded with XMLHttpRequest.
Martin Honnen