Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you use JQuery to transform XML to XML via XSLT

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
        });

    }
});
like image 975
Chris Avatar asked Jul 05 '11 05:07

Chris


People also ask

How do I convert one XML to another in XSLT?

The standard way to transform XML data into other formats is by Extensible Stylesheet Language Transformations (XSLT). You can use the built-in XSLTRANSFORM function to convert XML documents into HTML, plain text, or different XML schemas. XSLT uses stylesheets to convert XML into other data formats.

Does jQuery work with XML?

jQuery can be used for XML processing on the Web as well as HTML processing, and in this article I show some examples of this use. In developing code examples for this article I downloaded the uncompressed bundle of jQuery 1.2. 1 and tested on Firefox 2.0. 0.7.

Which is used to transform XML data based on the XSLT script?

XSLT stands for Extensible Stylesheet Language Transformation. XSLT is used to transform XML document from one form to another form. XSLT uses Xpath to perform matching of nodes to perform these transformation .

Can we use JavaScript in XSLT?

JavaScript can run XSLT transformations through the XSLTProcessor object. Once instantiated, an XSLTProcessor has an XSLTProcessor. importStylesheet() method that takes as an argument the XSLT stylesheet to be used in the transformation. The stylesheet has to be passed in as an XML document, which means that the .


3 Answers

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.

like image 125
Konstantin Avatar answered Oct 13 '22 23:10

Konstantin


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.

like image 28
Dennis Kreminsky Avatar answered Oct 14 '22 01:10

Dennis Kreminsky


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.

like image 24
cuq Avatar answered Oct 14 '22 00:10

cuq