# Updating HTML page through AJAX with XML response and XSL

**URL:** <https://www.ruby-forum.com/t/updating-html-page-through-ajax-with-xml-response-and-xsl/119641>\
**Category:** Rails\
**Created:** [November 8, 2007, 8:49pm UTC](https://www.ruby-forum.com/t/updating-html-page-through-ajax-with-xml-response-and-xsl/119641 "2007-11-08T20:49:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![christoph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@christoph](https://www.ruby-forum.com/u/christoph)\
**Post date:** [November 8, 2007, 8:49pm UTC](https://www.ruby-forum.com/t/updating-html-page-through-ajax-with-xml-response-and-xsl/119641/1 "2007-11-08T20:49:54Z")

</div>

Dear List,

I have a problem with nicely integrating XML responses with AJAX into  
a website. I did search the internet a lot but I did not find anything  
too useful; so I hope that my question is neither stupid nor trivial.

What I try to do:  
I have a ruby controller that produces some XML, say an RSS feed.  
Then, I have a “standard” HTML site that should integrate this RSS  
feed, e.g., by loading it from the server, applying some XSL  
transformation, and putting it into a div.

It would be even better if it would then be possible to do some AJAX  
magic, such as editing something which results in sending the changes  
to the server, retrieving the XML again, applying the XSL  
transformation and updating the page.

And it would be brilliant if it would also work into the other  
direction: instead of transforming a form into HTTP parameters that  
gets sent I would like to assemble some XML from it and send this. (by  
using some standard javascript library, if possible)

The problem is, that the Ruby AJAX helpers (and the browser) just take  
the XML as is and insert it into the site, without caring about any  
xsl-stylesheet tag. (Which is funny, since AJAX is about XML, not  
HTML:) Also, I want to (or have to) use XSL and not CSS, because I  
don’t know any way to transform e.g., an RSS link-tag into an HTML a-  
link in CSS. But as I understood, todays browsers do not all allow to  
perform XML transformations programmatically through javascript?

What I (kind of) managed to do is, to put some java script into  
the :loaded option of a Rails link\_to\_remote call. (code see below).  
This will load the xml and the xsl, do the transformation and append  
it to the document. I know that for several reasons this is a very bad  
piece of code, but it was meant to be a proof of concept.

My question to the community is now: Is there any elegant way to  
actually use XML with AJAX? Or is it only meant to work with text and  
maybe HTML snippets? (then I would like to make a request to remove  
the X and replace it with an H instead…) Or is it just the easiest  
to do the transformation on the server side? (which I wanted to avoid,  
but whatever…)

Any pointers are highly appreciated!

Thanks in advance,  
Christoph

— bad and broken example: —

Load RSS: \<%= link\_to\_remote "do it", :update =\> 'p1-detail', :method =\> 'get', :url =\> '/feed/recipes', :loaded =\> " var XSLT = new XSLTProcessor;

```
    var $xml = new XMLHttpRequest;
    $xml.open('GET', '/feeds/recipe/2', false);
    $xml.overrideMimeType('text/xml');
    $xml.send(null);
    var xml = $xml.responseXML;

    var $xsl = new XMLHttpRequest;
    $xsl.open('GET', '/stylesheets/smb_rss2html.xsl', false);
    $xsl.overrideMimeType('text/xml');
    $xsl.send(null);
    var xsl = $xsl.responseXML;

    XSLT.importStylesheet(xsl);

    document.body.
    appendChild(XSLT.transformToFragment(xml, document));"%>

```

---

<div class="post-metadata">

**Author:** ![christoph](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@christoph](https://www.ruby-forum.com/u/christoph)\
**Post date:** [November 8, 2007, 8:54pm UTC](https://www.ruby-forum.com/t/updating-html-page-through-ajax-with-xml-response-and-xsl/119641/2 "2007-11-08T20:54:35Z")

</div>

I like to be the first responder to my own questions:)

It turns out that I missed this thread:

[http://groups.google.com/group/rubyonrails-talk/browse\_thread/thread/cf7ebb10fca27cbc/082baedc0c983757?lnk=gst&q=xml+xsl#](http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/cf7ebb10fca27cbc/082baedc0c983757?lnk=gst&q=xml+xsl#)

However, no real results there; make it server-side and don’t rely on  
xsl on the client. So I guess AJAH would be more appropriate. But  
anyhow, maybe somebody has some great idea!?
