This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.

Compressing Whitespace in an XML Document
CodeExpectedActual
require 'rexml/document'
text = %{<doc><a>Some      whitespace</a>    <b>Some   more</b></doc>}
REXML::Document.new(text, { :compress_whitespace => :all }).to_s
"<doc><a>Some whitespace</a> <b>Some more</b></doc>" "<doc><a>Some whitespace</a> <b>Some more</b></doc>"
REXML::Document.new(text, { :compress_whitespace => %w{a} }).to_s
"<doc><a>Some whitespace</a> <b>Some more</b></doc>" "<doc><a>Some whitespace</a> <b>Some more</b></doc>"
REXML::Document.new(text, { :respect_whitespace => %w{a} }).to_s
"<doc><a>Some whitespace</a> <b>Some more</b></doc>" "<doc><a>Some whitespace</a> <b>Some more</b></doc>"
text = %{<doc><a>Some   text</a>\n  <b>Some   more</b>\n\n}
REXML::Document.new(text, { :compress_whitespace => :all }).to_s
"<doc><a>Some text</a>\n <b>Some more</b>\n</doc>" "<doc><a>Some text</a>\n <b>Some more</b>\n</doc>"
REXML::Document.new(text, { :compress_whitespace => :all,
	                    :ignore_whitespace_nodes => :all }).to_s
"<doc><a>Some text</a><b>Some more</b></doc>" "<doc><a>Some text</a><b>Some more</b></doc>"