XML Builder - why?

E

eastcoastcoder

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.
 
J

James Britt

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.ruby-doc.org - Ruby Help & Documentation
http://www.artima.com/rubycs/ - The Journal By & For Rubyists
http://www.rubystuff.com - The Ruby Store for Ruby Stuff
http://www.jamesbritt.com - Playing with Better Toys
http://www.30secondrule.com - Building Better Tools
 
D

David Holroyd

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
 
K

Keith Fahlgren

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/Transform" do
b.xsl :eek:utput, "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;`#{key}&apos;&quot;]" do
b.xsl :copy, "`#{value}'"
# b.xsl :message, "Matched #{key}"
end
} # end of outvalue.each
} # end of yaml.each
end
end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top