Namespaces, attributes and content in rxml

Rails’ RXML templates (powered by Builder) are a great way to generate various different types of XML output from your application, but the documentation could be lacking. I’ve been working with namespaced content for a while but I’ve spent quite a while today trying to work out how to add an element in an explicit namespace, with attributes and text content.

To create an ‘id’ element is straightforward:

xml.id 'my-id-here'

and then to put that tag in the ‘foo’ namespace you need:

xml.foo :id, 'my-id-here'

To create children of that element (say xhtml content in an atom feed) you need:

xml.content :type => 'xhtml' do
  xml.xhtml :div do
    xml.p entry.excerpt
  end
end

And the final piece of the puzzle, and the end to that particular bout of frustration is the text! method. To use my exact example:

xml.georss :point, :featuretypetag => 'wifi' do
  xml.text! entry.latitude + ' ' + entry.longitude
end

Tags: , , ,

Comments are closed.