# Ruby in XML

**URL:** <https://rubytalk.org/t/ruby-in-xml/19412>\
**Category:** ruby-talk\
**Created:** [8 July 2005 00:22 UTC](https://rubytalk.org/t/ruby-in-xml/19412 "2005-07-08T00:22:29Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Carter](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@John\_Carter](https://rubytalk.org/u/John_Carter)\
**Post date:** [8 July 2005 00:22 UTC](https://rubytalk.org/t/ruby-in-xml/19412/1 "2005-07-08T00:22:29Z")

</div>

I have just stuck this on..  
&nbsp;&nbsp;&nbsp;[http://www.rubygarden.org/ruby?RubyInXML](http://www.rubygarden.org/ruby?RubyInXML)

I like XML.

There is a firm standard, there is a rich toolset to work on it.

I like HTML. It is simply the fastest way to deliver good looking documents to the widest audience.

No surprise. I like XHTML it \_is\_ HTML in XML. I can validate my XHTML documents and know that they conform exactly to the standard, and hence will render properly on a wide set of browsers.

I love ruby. It is quite the easiest way to program. It has a lovely XML API called REXML.

I sometimes need to do spreadsheet sort of things. Basically a document that describes my reasoning and findings, supported by numbers.

Long time ago, when I still did Perl, I found by actual trials that I was about as fast in Perl as the average guy is using a Spreadsheet. Sometimes faster, sometimes slower. But for the next hundred data sets, my perl scripts where a thousand times faster.

So I don't do spreadsheets these days, I write ruby scripts.

So I have taken to combining Ruby & HTML. Sometimes via cgi. It works for me.

But sometimes I have documents that are more HTML than ruby. So it makes sense to write them in HTML, with a bit of Ruby embedded. That's where erb and eruby live.

But I don't like erb and eruby's tags. I can't validate my XHTML.

So add REXML and I present a very small script I call rubyexml. Ruby Embedded in XML.

#!/usr/bin/ruby -w

require 'rexml/document'  
require 'rexml/streamlistener'  
require 'pp'

# All eval's are evaluated in the context of an instance of this class.  
# Extend this, or add this method to a class of your own.  
class Context

&nbsp;&nbsp;&nbsp;def eval\_value( value)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value.gsub( %r{ \#\{ ( [^\}]+ ) \} }x) do | match|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_eval( $1).to\_s  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end  
end

# This does the work.  
class Listener  
&nbsp;&nbsp;&nbsp;include REXML::StreamListener

&nbsp;&nbsp;&nbsp;def initialize( context)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@context = context  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def comment( text)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print @context.instance\_eval( text)  
&nbsp;&nbsp;&nbsp;rescue SyntaxError =\> details  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pp @context  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pp text  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise "Failed to compile '#{text}' in context : #{details}"  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def tag\_start(name,attrs)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "\<",name  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;attrs.each\_pair do |key, value|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print " #{key}=\"#{@context.eval\_value( value)}\""  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "\>"  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def tag\_end( name)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "\</", name, "\>"  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def text( text)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print @context.eval\_value(text)  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def cdata( ctext)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text( ctext)  
&nbsp;&nbsp;&nbsp;end  
end

# This comes for free from REXML. Stream parse an XML document.  
REXML::Document::parse\_stream( REXML::SourceFactory::create\_from( STDIN), Listener::new( Context.new))

So take a chunk of XHTML...

\<?xml version="1.0"?\>  
\<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"  
"xhtml11.dtd" \>  
\<html xmlns="[HTTP://www.w3.org/TR/xhtml](HTTP://www.w3.org/TR/xhtml)"  
&nbsp;&nbsp;&nbsp;&nbsp;xmlns:xlink="[HTTP://www.w3.org/XML/XLink/0.9](HTTP://www.w3.org/XML/XLink/0.9)"  
&nbsp;&nbsp;&nbsp;&nbsp;xml:lang="en" \>  
&nbsp;&nbsp;&nbsp;&nbsp;\<head\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<title\>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</title\>  
&nbsp;&nbsp;&nbsp;&nbsp;\</head\>

&nbsp;&nbsp;&nbsp;&nbsp;\<body\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<h1\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The answer to life, the universe and everything is \<!-- 44 - 2 --\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</h1\>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<p\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The following image is \<!-- @file\_name = "pretty\_picture.jpg" --\>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<img src="#{@file\_name}" alt = "#{@file\_name.sub(/\.jpg/,'')}"/\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</p\>  
&nbsp;&nbsp;&nbsp;&nbsp;\</body\>  
\</html\>

It validates as correct xml against the XHTML DTD.

Feed it through rubyexml and get...

\<html xmlns:xlink="[HTTP://www.w3.org/XML/XLink/0.9](HTTP://www.w3.org/XML/XLink/0.9)" xml:lang="en" xmlns="[HTTP://www.w3.org/TR/xhtml](HTTP://www.w3.org/TR/xhtml)"\>  
&nbsp;&nbsp;&nbsp;&nbsp;\<head\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<title\>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</title\>  
&nbsp;&nbsp;&nbsp;&nbsp;\</head\>

&nbsp;&nbsp;&nbsp;&nbsp;\<body\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<h1\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The answer to life, the universe and everything is 42  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</h1\>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<p\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The following image is pretty\_picture.jpg

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<img src="pretty\_picture.jpg" alt="pretty\_picture"\>\</img\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</p\>  
&nbsp;&nbsp;&nbsp;&nbsp;\</body\>  
\</html\>

Just so blooming simple.

And if you have a big hairy object that knows all the deeper secrets of life, just change rubyexml to...

REXML::Document::parse\_stream( REXML::SourceFactory::create\_from( STDIN), Listener::new( BigHairyObjectThatKnowsTheDeeperSecretsOfLife.new))

And you can refer to all it's instance variables and methods.

It all so blooming simple!

John Carter Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : [john.carter@tait.co.nz](mailto:john.carter@tait.co.nz)  
New Zealand

"At first I hoped that such a technically unsound project would  
&nbsp;&nbsp;collapse but I soon realized it was doomed to success. Almost  
&nbsp;&nbsp;anything in software can be implemented, sold, and even used given  
&nbsp;&nbsp;enough determination. There is nothing a mere scientist can say that  
&nbsp;&nbsp;will stand against the flood of a hundred million dollars. But there  
&nbsp;&nbsp;is one quality that cannot be purchased in this way---and that is  
&nbsp;&nbsp;reliability. The price of reliability is the pursuit of the utmost  
&nbsp;&nbsp;simplicity. It is a price which the very rich find most hard to  
&nbsp;&nbsp;pay." -- C.A.R. Hoare in The Emperor's Old Clothes,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Turing Award Lecture (27 October 1980)

---

<div class="post-metadata">

**Author:** ![David\_Mitchell](https://avatars.discourse-cdn.com/v4/letter/d/2bfe46/32.png) [@David\_Mitchell](https://rubytalk.org/u/David_Mitchell)\
**Post date:** [8 July 2005 00:45 UTC](https://rubytalk.org/t/ruby-in-xml/19412/2 "2005-07-08T00:45:04Z")

</div>

How does this work for loops? For example this won't work:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<!-- for @i in 0...5 --\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<img src="#{@i}.jpg"/\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<!-- end --\>

How would you suggest I achieve this? Perhaps:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<!-- for @i in 0...5  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print "\<img src='#{@i}.jpg'/\>"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end --\>

I can quickly see that becoming an escaping nightmare.

I really like the idea but I would want it to be this flexible.

David

John Carter wrote:

> **···**
>
> > I have just stuck this on..  
> > &nbsp;&nbsp;[http://www.rubygarden.org/ruby?RubyInXML](http://www.rubygarden.org/ruby?RubyInXML)
> > 
> > I like XML.
> > 
> > There is a firm standard, there is a rich toolset to work on it.
> > 
> > I like HTML. It is simply the fastest way to deliver good looking documents to the widest audience.
> > 
> > No surprise. I like XHTML it \_is\_ HTML in XML. I can validate my XHTML documents and know that they conform exactly to the standard, and hence will render properly on a wide set of browsers.
> > 
> > I love ruby. It is quite the easiest way to program. It has a lovely XML API called REXML.
> > 
> > I sometimes need to do spreadsheet sort of things. Basically a document that describes my reasoning and findings, supported by numbers.
> > 
> > Long time ago, when I still did Perl, I found by actual trials that I was about as fast in Perl as the average guy is using a Spreadsheet. Sometimes faster, sometimes slower. But for the next hundred data sets, my perl scripts where a thousand times faster.
> > 
> > So I don't do spreadsheets these days, I write ruby scripts.
> > 
> > So I have taken to combining Ruby & HTML. Sometimes via cgi. It works for me.
> > 
> > But sometimes I have documents that are more HTML than ruby. So it makes sense to write them in HTML, with a bit of Ruby embedded. That's where erb and eruby live.
> > 
> > But I don't like erb and eruby's tags. I can't validate my XHTML.
> > 
> > So add REXML and I present a very small script I call rubyexml. Ruby Embedded in XML.
> > 
> > #!/usr/bin/ruby -w
> > 
> > require 'rexml/document'  
> > require 'rexml/streamlistener'  
> > require 'pp'
> > 
> > # All eval's are evaluated in the context of an instance of this class.  
> > # Extend this, or add this method to a class of your own.  
> > class Context
> > 
> > &nbsp;&nbsp;def eval\_value( value)  
> > &nbsp;&nbsp;&nbsp;&nbsp;value.gsub( %r{ \#\{ ( [^\}]+ ) \} }x) do | match|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_eval( $1).to\_s  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end  
> > end
> > 
> > # This does the work.  
> > class Listener  
> > &nbsp;&nbsp;include REXML::StreamListener
> > 
> > &nbsp;&nbsp;def initialize( context)  
> > &nbsp;&nbsp;&nbsp;&nbsp;@context = context  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def comment( text)  
> > &nbsp;&nbsp;&nbsp;&nbsp;print @context.instance\_eval( text)  
> > &nbsp;&nbsp;rescue SyntaxError =\> details  
> > &nbsp;&nbsp;&nbsp;&nbsp;pp @context  
> > &nbsp;&nbsp;&nbsp;&nbsp;pp text  
> > &nbsp;&nbsp;&nbsp;&nbsp;raise "Failed to compile '#{text}' in context : #{details}"  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def tag\_start(name,attrs)  
> > &nbsp;&nbsp;&nbsp;&nbsp;print "\<",name  
> > &nbsp;&nbsp;&nbsp;&nbsp;attrs.each\_pair do |key, value|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print " #{key}=\"#{@context.eval\_value( value)}\""  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;print "\>"  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def tag\_end( name)  
> > &nbsp;&nbsp;&nbsp;&nbsp;print "\</", name, "\>"  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def text( text)  
> > &nbsp;&nbsp;&nbsp;&nbsp;print @context.eval\_value(text)  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;def cdata( ctext)  
> > &nbsp;&nbsp;&nbsp;&nbsp;text( ctext)  
> > &nbsp;&nbsp;end  
> > end
> > 
> > # This comes for free from REXML. Stream parse an XML document.  
> > REXML::Document::parse\_stream( REXML::SourceFactory::create\_from( STDIN), Listener::new( Context.new))
> > 
> > So take a chunk of XHTML...
> > 
> > \<?xml version="1.0"?\>  
> > \<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"  
> > "xhtml11.dtd" \>  
> > \<html xmlns="[HTTP://www.w3.org/TR/xhtml&quot](HTTP://www.w3.org/TR/xhtml&quot);  
> > &nbsp;&nbsp;&nbsp;xmlns:xlink="[HTTP://www.w3.org/XML/XLink/0.9&quot](HTTP://www.w3.org/XML/XLink/0.9&quot);  
> > &nbsp;&nbsp;&nbsp;xml:lang="en" \>  
> > &nbsp;&nbsp;&nbsp;\<head\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<title\>
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;\</title\>  
> > &nbsp;&nbsp;&nbsp;\</head\>
> > 
> > &nbsp;&nbsp;&nbsp;\<body\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<h1\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The answer to life, the universe and everything is \<!-- 44 - 2 --\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</h1\>
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<p\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The following image is \<!-- @file\_name = "pretty\_picture.jpg" --\>
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<img src="#{@file\_name}" alt = "#{@file\_name.sub(/\.jpg/,'')}"/\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</p\>  
> > &nbsp;&nbsp;&nbsp;\</body\>  
> > \</html\>
> > 
> > It validates as correct xml against the XHTML DTD.
> > 
> > Feed it through rubyexml and get...
> > 
> > \<html xmlns:xlink="[HTTP://www.w3.org/XML/XLink/0.9&quot](HTTP://www.w3.org/XML/XLink/0.9&quot); xml:lang="en" xmlns="[HTTP://www.w3.org/TR/xhtml&quot;&gt](HTTP://www.w3.org/TR/xhtml&quot;&gt);  
> > &nbsp;&nbsp;&nbsp;\<head\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<title\>
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;\</title\>  
> > &nbsp;&nbsp;&nbsp;\</head\>
> > 
> > &nbsp;&nbsp;&nbsp;\<body\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<h1\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The answer to life, the universe and everything is 42  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</h1\>
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<p\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The following image is pretty\_picture.jpg
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<img src="pretty\_picture.jpg" alt="pretty\_picture"\>\</img\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</p\>  
> > &nbsp;&nbsp;&nbsp;\</body\>  
> > \</html\>
> > 
> > Just so blooming simple.
> > 
> > And if you have a big hairy object that knows all the deeper secrets of life, just change rubyexml to...
> > 
> > REXML::Document::parse\_stream( REXML::SourceFactory::create\_from( STDIN), Listener::new( BigHairyObjectThatKnowsTheDeeperSecretsOfLife.new))
> > 
> > And you can refer to all it's instance variables and methods.
> > 
> > It all so blooming simple!
> > 
> > John Carter Phone : (64)(3) 358 6639  
> > Tait Electronics Fax : (64)(3) 359 4632  
> > PO Box 1645 Christchurch Email : john.carter@tait.co.nz  
> > New Zealand
> > 
> > "At first I hoped that such a technically unsound project would  
> > collapse but I soon realized it was doomed to success. Almost  
> > anything in software can be implemented, sold, and even used given  
> > enough determination. There is nothing a mere scientist can say that  
> > will stand against the flood of a hundred million dollars. But there  
> > is one quality that cannot be purchased in this way---and that is  
> > reliability. The price of reliability is the pursuit of the utmost  
> > simplicity. It is a price which the very rich find most hard to  
> > pay." -- C.A.R. Hoare in The Emperor's Old Clothes,  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Turing Award Lecture (27 October 1980)
> 
> --  
> David Mitchell  
> Software Engineer  
> Telogis

---

<div class="post-metadata">

**Author:** ![James\_Britt4](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@James\_Britt4](https://rubytalk.org/u/James_Britt4)\
**Post date:** [8 July 2005 01:26 UTC](https://rubytalk.org/t/ruby-in-xml/19412/3 "2005-07-08T01:26:24Z")

</div>

John Carter wrote:  
...

> So add REXML and I present a very small script I call rubyexml. Ruby

...

> So take a chunk of XHTML...
> 
> \<?xml version="1.0"?\>  
> \<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"  
> "xhtml11.dtd" \>

...

> &nbsp;&nbsp;&nbsp;\<body\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<h1\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The answer to life, the universe and everything is \<!-- 44 - 2 --\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</h1\>

Question: If your document has instructions for processing, why not use processing instructions? Why munge the semantics of the comments syntax?

Maybe take a look at how Nitro does this.

> **···**
>
> --
> 
> [http://www.ruby-doc.org](http://www.ruby-doc.org) - The Ruby Documentation Site  
> [http://www.rubyxml.com](http://www.rubyxml.com) - News, Articles, and Listings for Ruby & XML  
> [http://www.rubystuff.com](http://www.rubystuff.com) - The Ruby Store for Ruby Stuff  
> [http://www.jamesbritt.com](http://www.jamesbritt.com) - Playing with Better Toys

---

<div class="post-metadata">

**Author:** ![Daniel\_Brockman](https://avatars.discourse-cdn.com/v4/letter/d/ed8c4c/32.png) [@Daniel\_Brockman](https://rubytalk.org/u/Daniel_Brockman)\
**Post date:** [9 July 2005 01:04 UTC](https://rubytalk.org/t/ruby-in-xml/19412/4 "2005-07-09T01:04:43Z")

</div>

John Carter \<john.carter@tait.co.nz\> writes:

> I can validate my XHTML documents and know that they conform exactly to  
> the standard, and hence will render properly on a wide set of browsers.

If only it were that simple...

> **···**
>
> --  
> Daniel Brockman \<daniel@brockman.se\>

---

<div class="post-metadata">

**Author:** ![John\_Carter](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@John\_Carter](https://rubytalk.org/u/John_Carter)\
**Post date:** [8 July 2005 01:04 UTC](https://rubytalk.org/t/ruby-in-xml/19412/5 "2005-07-08T01:04:13Z")

</div>

> How does this work for loops? For example this won't work:

Yup. Thought about it. Didn't come up with any bright thunks..

\<!--  
&nbsp;&nbsp;&nbsp;(0...5).inject('') {|m,i|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m+= "\<img src=\"#{i}.jpg\"/\>"  
&nbsp;&nbsp;&nbsp;}  
--\>

Not bright, but will work.

> I really like the idea but I would want it to be this flexible.

Given flexible or simple, I chose simple.

Possibly this is merely a lack of imagination on my part.

Perhaps flexible and simple is possible. I wanted it to be able to validate as vanilla XHTML.

But that is what Wiki's are for. If I missed something, click on "edit this page".

> John Carter wrote:
> 
> > I have just stuck this on..  
> > &nbsp;&nbsp;[http://www.rubygarden.org/ruby?RubyInXML](http://www.rubygarden.org/ruby?RubyInXML)

John Carter Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : john.carter@tait.co.nz  
New Zealand

Carter's Compass...

&nbsp;&nbsp;&nbsp;&nbsp;I know I'm on the right track when by deleting code I'm adding  
&nbsp;&nbsp;&nbsp;&nbsp;functionality.

> **···**
>
> On Fri, 8 Jul 2005, David Mitchell wrote:

---

<div class="post-metadata">

**Author:** ![John\_Carter](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@John\_Carter](https://rubytalk.org/u/John_Carter)\
**Post date:** [8 July 2005 04:06 UTC](https://rubytalk.org/t/ruby-in-xml/19412/6 "2005-07-08T04:06:36Z")

</div>

> Question: If your document has instructions for processing, why not use processing instructions? Why munge the semantics of the comments syntax?

The short answer is I had read the XML standard so long ago I forgot about them....

I knew they existed, but I feared they had some deep meaning I didn't want to clash with.

I will change to using them.

Thank you,

> Maybe take a look at how Nitro does this.

Will do.

John Carter Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : john.carter@tait.co.nz  
New Zealand

Carter's Clarification of Murphy's Law.

"Things only ever go right so that they may go more spectacularly wrong later."

From this principle, all of life and physics may be deduced.

> **···**
>
> On Fri, 8 Jul 2005, James Britt wrote:

---

<div class="post-metadata">

**Author:** ![David\_Mitchell](https://avatars.discourse-cdn.com/v4/letter/d/2bfe46/32.png) [@David\_Mitchell](https://rubytalk.org/u/David_Mitchell)\
**Post date:** [8 July 2005 02:32 UTC](https://rubytalk.org/t/ruby-in-xml/19412/7 "2005-07-08T02:32:28Z")

</div>

Hey,

John Carter wrote:

> > I really like the idea but I would want it to be this flexible.
> 
> Given flexible or simple, I chose simple.  
> ..  
> Perhaps flexible and simple is possible. I wanted it to be able to validate as vanilla XHTML.

Ok, I don't think the example I posted falls outside the bounds of simple. Maybe it makes your script a little more complicated but not the syntax that the end user must use. I suspect if you hold off the actual evaluation of the comments until the page is output then you might find this task simpler. That is, build a single ruby script in memory that contains the logic for outputting the page, then just eval it at the end.

> **···**
>
> --  
> David Mitchell  
> Software Engineer  
> Telogis

---

<div class="post-metadata">

**Author:** ![George\_Moschovitis](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@George\_Moschovitis](https://rubytalk.org/u/George_Moschovitis)\
**Post date:** [9 July 2005 05:35 UTC](https://rubytalk.org/t/ruby-in-xml/19412/8 "2005-07-09T05:35:50Z")

</div>

Hello,

Nitro allready implements this. Have a look at [www.nitrohq.com](http://www.nitrohq.com).

The normal way to do this is:

\<ul\>  
\<?r for item in items ?\>  
&nbsp;&nbsp;\<li\>#{item.title}\</li\>  
\<?r end ?\>  
\</ul\>

If you include the morphing shader you can also write it as:

\<ul\>  
&nbsp;&nbsp;\<li each="item in items"\>#{item.title}\</li\>  
\</ul\>

And since Nitro, always gives you one more option (sic), you can do:

\<ul\>  
&nbsp;&nbsp;\<% for item in items %\>  
&nbsp;&nbsp;\<li\>#{item.title}\</li\>  
&nbsp;&nbsp;\<% end %\>  
\</ul\>

Suit yourself 😉

Of course Nitro can do so much more. You can use XSLT on top of you  
xhtml page, or the new cool Elements system (similar to JSP tag  
libraries).

regards,  
George.

---

<div class="post-metadata">

**Author:** ![John\_Carter](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@John\_Carter](https://rubytalk.org/u/John_Carter)\
**Post date:** [8 July 2005 03:51 UTC](https://rubytalk.org/t/ruby-in-xml/19412/9 "2005-07-08T03:51:21Z")

</div>

I guess where I started was I wanted to loop on the rows of a table.

\<table\>  
&nbsp;&nbsp;&nbsp;\<!-- (0..5).each { |i| --\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<tr\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#{i} - \<!-- @name[i] --\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</td\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<img src="#{i}.jpg" alt="#{i}"/\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</td\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</tr\>  
&nbsp;&nbsp;&nbsp;\<!-- } --\>  
\</table\>

But then I'm holding all kind of state and what happens if I want to iterate over the columns of the table as well? (Nested loops.)

\<table\>  
&nbsp;&nbsp;&nbsp;\<!-- (0..5).each { |i| --\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<tr\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#{i} - \<!-- @name[i] --\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</td\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<!-- (0..6).each do |j| --\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#{i\*10 + j}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</td\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<!-- end --\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</tr\>  
&nbsp;&nbsp;&nbsp;\<!-- } --\>  
\</table\>

I'm then operating a fairly hairy state machine in my Listener class or I'm no longer using the one (longish) line StreamParser.

However, if anyone gets the urge to embellish what I have done, as I say, that's what Wiki's are for.

John Carter Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : john.carter@tait.co.nz  
New Zealand

Carter's Clarification of Murphy's Law.

"Things only ever go right so that they may go more spectacularly wrong later."

From this principle, all of life and physics may be deduced.

> **···**
>
> On Fri, 8 Jul 2005, David Mitchell wrote:
> 
> > Ok, I don't think the example I posted falls outside the bounds of simple. Maybe it makes your script a little more complicated but not the syntax that the end user must use. I suspect if you hold off the actual evaluation of the comments until the page is output then you might find this task simpler.

---

<div class="post-metadata">

**Author:** ![Christian\_Neukirche1](https://avatars.discourse-cdn.com/v4/letter/c/ee7513/32.png) [@Christian\_Neukirche1](https://rubytalk.org/u/Christian_Neukirche1)\
**Post date:** [9 July 2005 12:33 UTC](https://rubytalk.org/t/ruby-in-xml/19412/10 "2005-07-09T12:33:34Z")

</div>

"George Moschovitis" \<george.moschovitis@gmail.com\> writes:

> And since Nitro, always gives you one more option (sic), you can do:
> 
> \<ul\>  
> &nbsp;&nbsp;\<% for item in items %\>  
> &nbsp;&nbsp;\<li\>#{item.title}\</li\>  
> &nbsp;&nbsp;\<% end %\>  
> \</ul\>

Except that isn't valid XML anymore; maybe a templating library  
along Amrita2 or XTemplate would be more appropriate?

One could imagine a processing instruction at the beginning to setup  
the contents, and then let Amrita do all the stuff.

> **···**
>
> > George.
> 
> --  
> Christian Neukirchen \<chneukirchen@gmail.com\> [http://chneukirchen.org](http://chneukirchen.org)

---

<div class="post-metadata">

**Author:** ![David\_Mitchell](https://avatars.discourse-cdn.com/v4/letter/d/2bfe46/32.png) [@David\_Mitchell](https://rubytalk.org/u/David_Mitchell)\
**Post date:** [8 July 2005 05:13 UTC](https://rubytalk.org/t/ruby-in-xml/19412/11 "2005-07-08T05:13:17Z")

</div>

I hate to break this to you John, but the link you posted is to a blank wiki page. Perhaps you got the wrong link? The page [http://www.rubygarden.org/ruby?RubyInXML](http://www.rubygarden.org/ruby?RubyInXML) doesn't exist.

Yes, you end up with a hairy state machine, but it doesn't mean you lose the one-line StreamParser.

Cheers

David

John Carter wrote:

> **···**
>
> > On Fri, 8 Jul 2005, David Mitchell wrote:
> > 
> > > Ok, I don't think the example I posted falls outside the bounds of simple. Maybe it makes your script a little more complicated but not the syntax that the end user must use. I suspect if you hold off the actual evaluation of the comments until the page is output then you might find this task simpler.
> > 
> > I guess where I started was I wanted to loop on the rows of a table.
> > 
> > \<table\>  
> > &nbsp;&nbsp;\<!-- (0..5).each { |i| --\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;\<tr\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#{i} - \<!-- @name[i] --\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</td\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<img src="#{i}.jpg" alt="#{i}"/\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</td\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;\</tr\>  
> > &nbsp;&nbsp;\<!-- } --\>  
> > \</table\>
> > 
> > But then I'm holding all kind of state and what happens if I want to iterate over the columns of the table as well? (Nested loops.)
> > 
> > \<table\>  
> > &nbsp;&nbsp;\<!-- (0..5).each { |i| --\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;\<tr\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#{i} - \<!-- @name[i] --\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</td\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<!-- (0..6).each do |j| --\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<td\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#{i\*10 + j}  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</td\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<!-- end --\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;\</tr\>  
> > &nbsp;&nbsp;\<!-- } --\>  
> > \</table\>
> > 
> > I'm then operating a fairly hairy state machine in my Listener class or I'm no longer using the one (longish) line StreamParser.
> > 
> > However, if anyone gets the urge to embellish what I have done, as I say, that's what Wiki's are for.
> > 
> > John Carter Phone : (64)(3) 358 6639  
> > Tait Electronics Fax : (64)(3) 359 4632  
> > PO Box 1645 Christchurch Email : john.carter@tait.co.nz  
> > New Zealand
> > 
> > Carter's Clarification of Murphy's Law.
> > 
> > "Things only ever go right so that they may go more spectacularly wrong later."
> > 
> > > From this principle, all of life and physics may be deduced.
> 
> --  
> David Mitchell  
> Software Engineer  
> Telogis

---

<div class="post-metadata">

**Author:** ![George\_Moschovitis](https://avatars.discourse-cdn.com/v4/letter/g/278dde/32.png) [@George\_Moschovitis](https://rubytalk.org/u/George_Moschovitis)\
**Post date:** [11 July 2005 09:50 UTC](https://rubytalk.org/t/ruby-in-xml/19412/12 "2005-07-11T09:50:49Z")

</div>

> Except that isn't valid XML anymore; maybe a templating library  
> along Amrita2 or XTemplate would be more appropriate?

Yeap, it isn't valid XTML and this is discouraged. I find this usefull  
when defining for example Email templates. Or if you would like to use  
some Rails code with out many changes...

---

<div class="post-metadata">

**Author:** ![John\_Carter](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@John\_Carter](https://rubytalk.org/u/John_Carter)\
**Post date:** [8 July 2005 05:39 UTC](https://rubytalk.org/t/ruby-in-xml/19412/13 "2005-07-08T05:39:58Z")

</div>

> I hate to break this to you John, but the link you posted is to a blank wiki page. Perhaps you got the wrong link? The page [http://www.rubygarden.org/ruby?RubyInXML](http://www.rubygarden.org/ruby?RubyInXML) doesn't exist.

Nope, seems to be all there when I look. From two separate machines.

> Yes, you end up with a hairy state machine, but it doesn't mean you lose the one-line StreamParser.

True. I meant the choice was a hairy state machine xor use the "eat whole doc" REXML parser instead of stream parser.

John Carter Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : john.carter@tait.co.nz  
New Zealand

Carter's Clarification of Murphy's Law.

"Things only ever go right so that they may go more spectacularly wrong later."

From this principle, all of life and physics may be deduced.

> **···**
>
> On Fri, 8 Jul 2005, David Mitchell wrote:

---

<div class="post-metadata">

**Author:** ![Dominik\_Bathon](https://avatars.discourse-cdn.com/v4/letter/d/67e7ee/32.png) [@Dominik\_Bathon](https://rubytalk.org/u/Dominik_Bathon)\
**Post date:** [8 July 2005 11:11 UTC](https://rubytalk.org/t/ruby-in-xml/19412/14 "2005-07-08T11:11:27Z")

</div>

You have probably been caught by the RubyGarden tarpit:

[http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/137405](http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/137405)

> **···**
>
> On Fri, 08 Jul 2005 07:13:17 +0200, David Mitchell \<david.mitchell@telogis.com\> wrote:
> 
> > I hate to break this to you John, but the link you posted is to a blank wiki page. Perhaps you got the wrong link? The page [http://www.rubygarden.org/ruby?RubyInXML](http://www.rubygarden.org/ruby?RubyInXML) doesn't exist.

---

<div class="post-metadata">

**Author:** ![John\_Carter](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@John\_Carter](https://rubytalk.org/u/John_Carter)\
**Post date:** [10 July 2005 21:05 UTC](https://rubytalk.org/t/ruby-in-xml/19412/15 "2005-07-10T21:05:09Z")

</div>

Hmm. Unfortunately I live behind a large corporate firewall. So reverse DNS is never going to work right.

I tried putting in other wiki links to that page and found that I had to wade through the entire existing page and find every existing http: and convert it to HTTP:.

I gave up on that pretty fast.

John Carter Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : john.carter@tait.co.nz  
New Zealand

Carter's Clarification of Murphy's Law.

"Things only ever go right so that they may go more spectacularly wrong later."

From this principle, all of life and physics may be deduced.

> **···**
>
> On Fri, 8 Jul 2005, Dominik Bathon wrote:
> 
> > On Fri, 08 Jul 2005 07:13:17 +0200, David Mitchell \> \<david.mitchell@telogis.com\> wrote:
> > 
> > > I hate to break this to you John, but the link you posted is to a blank wiki page. Perhaps you got the wrong link? The page [http://www.rubygarden.org/ruby?RubyInXML](http://www.rubygarden.org/ruby?RubyInXML) doesn't exist.
> > 
> > You have probably been caught by the RubyGarden tarpit:
> > 
> > [http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/137405](http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/137405)

---

<div class="post-metadata">

**Author:** ![Jim\_Weirich1](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@Jim\_Weirich1](https://rubytalk.org/u/Jim_Weirich1)\
**Post date:** [11 July 2005 01:02 UTC](https://rubytalk.org/t/ruby-in-xml/19412/16 "2005-07-11T01:02:04Z")

</div>

> Hmm. Unfortunately I live behind a large corporate firewall. So reverse  
> DNS is never going to work right.

As long as it resolves back to your corporate firewall, it should be OK. I  
live behind an extremely unfriendly firewall, and it causes no problems for  
the wiki.

And if it is the case that it does cause problem, just define a preferences  
setting (which sets a cookie in your browser). That will make you avoid the  
tarpit as well.

> I tried putting in other wiki links to that page and found that I  
> had to wade through the entire existing page and find every existing http:  
> and convert it to HTTP:.

Yea, that is pretty annoying. I disabled that feature tonight. Although  
helpful in the short run, it did nothing for long term spam avoidance. You  
should be able to post links using http: again.

> **···**
>
> On Sunday 10 July 2005 05:05 pm, John Carter wrote:
> 
> --  
> -- Jim Weirich jim@weirichhouse.org [http://onestepback.org](http://onestepback.org)  
> -----------------------------------------------------------------  
> "Beware of bugs in the above code; I have only proved it correct,  
> not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)
