7

I have a website that has links to documents that are dynamically populated based on the document type and all the data is located in one central xml file. I wanted have JQuery pass a parameter to the style sheet, the style sheet segregate out the the nodes using xpath based on the passed parameter, and then sort the notes based on an attribute. From all the documentation I found, JQuery doesn't natively support XSLT and none of the 3rd party plugins can return a new XML object once the original xml has been transformed. Am I missing something or is what I'm trying to do not possible? The xsl file has been tested outside of javascript and it works flawlessly.

Here is a sample of the code without the transform

$.ajax({
            type: "GET",
    url: "xml/charts.xml",
    dataType: "xml",
    success: function(xml) {        

        $(xml).find('chart').each(function(){
            // Create link here
        });

    }
});
Cœur
  • 37,241
  • 25
  • 195
  • 267
Chris
  • 71
  • 1
  • 1
  • 3

3 Answers3

3

Another one is jquery.xslTransform on http://jquery.glyphix.com/jquery.xslTransform/example/index.html

// now load both files into variables for the next 2 transformations
var xsldoc = $.xsl.load('test.xsl');
var xmldoc = $.xsl.load('test.xml');

// with an xpath
$('#with').getTransform(
    xsldoc,
    xmldoc,
    {
        xpath: '/test/inside'
    }
);

Or as general documentation states:

$.getTransform(
'path-to-xsl.xsl',              // path or xsl document in javascript variable
'path-to-xml.xml',              // path or xml document in javascript variable
{
  params: {                     // object for your own xsl parameters
    paramName1: 'paramValue1',
    paramName2: 'paramValue2'
  },
  xpath: '/test/inside',        // trims your xml file to that defined by this xpath
  eval: true,                   // evaluates any <script> blocks it finds in the transformed result
  callback: function(){}        // getTransform evaluates this function when transformation is complete
});

// loads an xml file, parses it and stores it in xmlDoc
var xmlDoc = xslTransform.load('path-to-xml.xml');

There's an example of usage on the linked page, guess it can suit your needs although it's a javascript wrapper of sarissa which is trying to make a browser-independent API for XSL tools in all browsers.

Konstantin
  • 113
  • 6
1

You can do XSLT transformations in Javascript, jQuery is not even involved in this process, however I seriously doubt that you would be able to pass any parameters to the processor.

There is a tutorial on XSLT processing using javascript on w3schools.

Dennis Kreminsky
  • 2,117
  • 15
  • 23
  • Why would you seriously doubt that you'd be able to pass parameters? https://developer.mozilla.org/en/Using_the_Mozilla_JavaScript_interface_to_XSL_Transformations#Setting_parameters – Wayne Jul 05 '11 at 14:49
  • because there's no standardized way to do it, at least as far as I know. nice to see that firefox supports it, of course. – Dennis Kreminsky Jul 05 '11 at 18:48
  • w3schools solution assumes activeX objects and is lifted from Microsoft's site. – JSDBroughton Jul 12 '13 at 16:31
  • Actually the w3schools soultion uses the standard method of doing an ajax callback before it was commonly known as ajax: make the callback with an activeX object if you're on IE, otherwise use the XMLHttpRequest object. – lfalin Oct 09 '13 at 12:48
1

A more portable implementation is ajaxslt ( http://goog-ajaxslt.sourceforge.net/ ), it is limited but it works fine in many situations. I used it some time ago for a proyect and it worked even in explorer 6.

cuq
  • 21
  • 3