Thursday, April 24, 2008

Rending your XML as HTML with XSLT in Rails

I had a need to convert XML services that my rails app was providing into customizable html widgets tht could then be in-jested by some sort of server side includes(like NGINX's SSI). So I needed to use XSLT.

When the W3C wrote the XML specification they also developed the Extensible Stylesheet Language for Transformations. XSLT provides a powerful, flexible language for transforming XML documents into something else. In this case, we're going to use it to create HTML documents.

So to quickly look at how we're rendering xml currently.


def show
@event = Event.find(params[:id])
render :xml => @event
end

OK so we are simply rendering only xml back from this request. Now of course we can react to any registered mime-type we want, obviously HTML is one of those. So we do something like....

def show
@event = Event.find(params[:id])
responds_to do |format|
format.html
format.xml { render :xml => @event }
end
end

So, I'm going to assume that you've seen this responds to block before and you know that it's just going to render the show.html.erb template in our views, when html is requested.

Installing Ruby's XSLT Library


I'm on Mac OS-X 10.5.2 and haven't tested this install on anything else, but it's pretty simple really, unfortunately it's not a gem, but hey, it's not that hard

First you'll need to download the library, you can get it from here

Unzip that guy and in your terminal navigate to that directory, then execute the following commands(You'll need Make installed so ensure you've XCode installed on OS-X, or some sort of GCC/Make):
ruby extconf.rb
make
make test
make doc
sudo make install

Fire up irb and ensure you return true from require 'xml/xslt' if you get a false, the library hasn't installed properly, you can probably find help at http://greg.rubyfr.net/pub/packages/ruby-xslt/files/README.html

Getting Rails to render:


Firstly, include your new friend in environment.rb
require 'xml/xslt'

In my application controller I've added a private method

def xslt(_xml, _xslt)
xlt = XML::XSLT.new
xlt.xml = render_to_string :xml => _xml
xlt.xsl = _xslt
xlt.serve
end

Now in your controller method(I'll use the example from above)

def show
@event = Event.find(params[:id])
respond_to do |format|
format.html { render :inline => xslt(@event, "#{RAILS_ROOT}/xslt_template/show.xslt") }
format.xml { render :xml => @event }
end
end

Create an XSLT Style


You're of course going to need XSLT styles to apply to pages.
I'll start with the XML I'm using. It's just rendered by the show method from above:

<event>
<created-at type="datetime">2008-04-23T18:36:28+10:00</created-at>
<date type="date">2008-04-23</date>
<id type="integer">1</id>
<location>Camperdown Park</location>
<title>Cardboard Tube Fighting</title>
<updated-at type="datetime">2008-04-23T18:36:28+10:00</updated-at>
</event>

And this is the XSLT template I'll use to style this.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />

<xsl:template match="/">
<xsl:apply-templates select="event/location" />
<xsl:apply-templates select="event/title" />
</xsl:template>

<xsl:template match="title">
<h4>
<xsl:value-of select="." />
</h4>
</xsl:template><br />
<xsl:template match="location">
<h1>
<xsl:value-of select="." />
</h1>
</xsl:template>
</xsl:stylesheet>

Smile like a bandit!


terminal: script/server
Navigate to the path of your method and voila you've just applied an XSLT style to your XML stream dynamically.

Next is for me to roll this into a Rail templating language... But that's another post.

3 comments:

Anonymous said...

Thanks for the post, been looking for something along these lines.

Anonymous said...

I had to change a couple things to get it to work in rails 2.0.2.

In the show method, I needed to use respond_to instead of responds_to.

In the xslt method, I needed to use xlt.xsl = _xslt instead of xlt.xsl = _xsl. Not sure how that could work otherwise.

Cameron Barrie said...

Thanks pete, right you are, typo's on my behalf... Should have just copied and pasted code. :D

Fixed the post now in any case, thanks again.