tags:

views:

128

answers:

2

Here is the problem:

I'm looping over a set of nodes and based on their type i'd like to use the jQuery xslt plugin.

var options = { 
    type: "POST",
    url: "api/dosomething/usefull",
    data: "orderid=12345",
    success: function(response) { 
        $(response).find("group").each(function() {
            if ($(this).attr(type) == "X") {
       $.xslt({xml: $(this), xslUrl: 'xsl/order/x_group.xsl', xslCache: false, callback: function(data){ 
        //do something usefull with the transformed data
        }});                        
            } else {
       $.xslt({xml: $(this), xslUrl: 'xsl/order/other_group.xsl', xslCache: false, callback: function(data){ 
        //do else with the transformed data
        }});                             
            }
        })
    }
};

The problem is that the the xslt plugin expects a javascript document object. How can I transform the result of the each function back into a document? $(this).text() will strip all inner xml tags. $(this).html() won't work either, since XML under jQuery doesn't support that. Plainly using this as parameter (ofcourse) triggers an error.

Alternatives considerd:

  • Using the javascript inbuilt document parsing functions to reduce the document to the nodes we need and then throw hem through the xslt parser. Given the ease of use of jQuery, i'd rather not do that though.
  • Throw the whole document into the xslt parser and solve it in the xslt. This would be an option, if the parser (or the xslt parser from the browser we're targeting instead of the pluign) would support xsl:import. We need to reuse these xslt's
  • I could create a new document, if only i knew how to get the internal string representation...
A: 

Can you convert $(this) back to native DOM using $(this).get(0)?

Then at least you'd be passing in the native element then a jQuery Collection.

RedWolves
using console.log($(this).get(0)) gives me exactly the same object as console.log(this). I've tried to throw it into the xslt parser anyway, but no luck...
Frank de Weger
A: 

More googling solved the problem:

 var xmldoc = ((new XMLSerializer()).serializeToString(this));

I can now use xmldoc as an argument to the xslt engine :)

Frank de Weger