[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

XML Builder - why?

eastcoastcoder

2/16/2006 6:29:00 AM

I see that a lot of Rubyists like using XML Builder to generate XML.
why took this even further with markaby to generate HTML.

My question is: why? In what way is Builder any easier, clearer, more
succint than simpling using an ERB template? I find that Builder is
about the same length, with about the same structure, only introducing
an extra conversion. For what?

The reason I'm asking is that I see that a lot of bright, experienced
Ruby programmers use it, and I'm wondering if I'm missing something or
not.

4 Answers

james_b

2/16/2006 7:47:00 AM

0

eastcoastcoder@gmail.com wrote:
> I see that a lot of Rubyists like using XML Builder to generate XML.
> why took this even further with markaby to generate HTML.
>
> My question is: why? In what way is Builder any easier, clearer, more
> succint than simpling using an ERB template? I find that Builder is
> about the same length, with about the same structure, only introducing
> an extra conversion. For what?

Uses plain Ruby. No '<' '>" thingies. Automagic encoding of special
characters. Less chance of error. Focus on structure and logic, not
incidentals of the markup format.

Sure, for smallish things it may be overkill, but otherwise it is quite
handy, especially for the angle-bracket-phobic

>
> The reason I'm asking is that I see that a lot of bright, experienced
> Ruby programmers use it, and I'm wondering if I'm missing something or
> not.

Perhaps not. Season to taste.


--
James Britt

http://www.ru... - Ruby Help & Documentation
http://www.artima.c... - The Journal By & For Rubyists
http://www.rub... - The Ruby Store for Ruby Stuff
http://www.jame... - Playing with Better Toys
http://www.30seco... - Building Better Tools


David Holroyd

2/16/2006 1:23:00 PM

0

On Thu, Feb 16, 2006 at 03:33:27PM +0900, eastcoastcoder@gmail.com wrote:
> I see that a lot of Rubyists like using XML Builder to generate XML.
> why took this even further with markaby to generate HTML.
>
> My question is: why? In what way is Builder any easier, clearer, more
> succint than simpling using an ERB template? I find that Builder is
> about the same length, with about the same structure, only introducing
> an extra conversion. For what?

I like that the ruby parser checks the well-formedness of my markup for
me.


dave

--
http://david.holr...


Keith Fahlgren

2/16/2006 1:40:00 PM

0

On Thursday 16 February 2006 1:33 am, eastcoastcoder@gmail.com wrote:
> I see that a lot of Rubyists like using XML Builder to generate XML.
> why took this even further with markaby to generate HTML.

Well, I don't really use Builder for HTML, but generate quite a lot of
XML with it. Here's one simple use I like (make XSLT search-and-replace
with simple YAML):

#!/usr/bin/env ruby
require 'yaml'
require 'rubygems'
require_gem 'builder', '~> 1.2'

ARGV.each do |arg|
yml = YAML::load(File.open(arg))

b = Builder::XmlMarkup.new(:target => STDOUT,
:indent => 2)
b.xsl :stylesheet, "version" =>"1.0",
"xmlns:xsl"
=>"http://www.w3.org/1999/XSL/Trans... do
b.xsl :output, "method" =>"xml", "encoding"=>"ascii",
"cdata-section-elements=>"_facet"

b.comment!("Default Rule")
b.xsl :template, "match"=>"@*|node()" do
b.xsl :copy do
b.xsl :"apply-templates", "select"=>"@*|node()"
end # xsl:template
end # xsl:copy

yml.each { |outkey, outvalue|
outvalue.each { |key, value|
b.xsl :template, "match"=>"#{outkey}[. =
&quot;&#96;#{key}&apos;&quot;]" do
b.xsl :copy, "`#{value}'"
# b.xsl :message, "Matched #{key}"
end
} # end of outvalue.each
} # end of yaml.each
end
end


MenTaLguY

2/16/2006 5:50:00 PM

0

Quoting eastcoastcoder@gmail.com:

> The reason I'm asking is that I see that a lot of bright,
> experienced Ruby programmers use it, and I'm wondering if I'm
> missing something or not.

Perhaps because it's straight Ruby code -- you can apply the same
sorts of transformations/refactorings to your templates as you can
the rest of your coe.

-mental