Instead of improving my other answer, I would rather create a new one, since I basically need to rewrite it to work the way you want, and the old answer may help people for other reasons ...
So, after extracting the HTML string from the XML associated with you, you can continue it as follows:
// First we'll extract out the parts of the HTML-string that we need. var jq_html = $(html_from_xml); var style = jq_html.find('head').find('style').html(); var style_string = style.toString(); var table_and_stuff = jq_html.find('body').html(); // If you want to place it into the existing page DOM, // we need to place it inside a new DIV with a known ID... $('body').append('<div id="new_stuff"></div>'); $('#new_stuff').html(table_and_stuff); // Now, we need to re-write the styles so that they only // affect the content of the 'new_stuff' DIV: styles_array = style_string.split('}'); var new_styles = ''; $.each(styles_array,function(i) { if(i<(styles_array.length-1)) { new_styles += "#new_stuff "+styles_array[i]+"}"; } }) $('head').append('<style type="text/css">'+new_styles+'</style>');
And it really should be. The trick is that CSS will choose the most specific style for this case. So, if you have a <td> inside the div "newstuff", it will get the style from the new stylesheet. If you have a <td> outside of this div "newstuff", it will get the old style.
Hope this solves your problem.
source share