开发者

Converting jQuery objects back to xmldocument

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 brow开发者_Go百科ser 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...


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.


More googling solved the problem:

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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜