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.

Substituting XML Entities
CodeExpectedActual
require 'rexml/document'
str = %{<?xml version="1.0"?>
<!DOCTYPE doc [
 <!ENTITY product 'Stargaze'>
 <!ENTITY version '2.3'>
]>
<doc>
 &product; v&version; is the most advanced astronomy product on the market.
</doc>}
doc = REXML::Document.new str
doc.root.children[0].value
"\n Stargaze v2.3 is the most advanced astronomy product on the market.\n" "\n Stargaze v2.3 is the most advanced astronomy product on the market.\n"
doc.root.text
"\n Stargaze v2.3 is the most advanced astronomy product on the market.\n" "\n Stargaze v2.3 is the most advanced astronomy product on the market.\n"
doc.root.children[0].to_s
"\n &product; v&version; is the most advanced astronomy product on the market.\n" "\n &product; v&version; is the most advanced astronomy product on the market.\n"
doc.root.write
<doc>
&product; v&version; is the most advanced astronomy program on the market.
</doc>
<doc>
 &product; v&version; is the most advanced astronomy product on the market.
</doc>
require 'delegate'
require 'rexml/text'
class EntitySubstituter < DelegateClass(IO)
  def initialize(io, document, filter=nil)
    @document = document
    @filter = filter
    super(io)
  end
  def <<(s)
    super(REXML::Text::unnormalize(s, @document.doctype, @filter))
  end
end
output = EntitySubstituter.new($stdout, doc)
doc.write(output)
<?xml version='1.0'?><!DOCTYPE doc [
<!ENTITY product "Stargaze">
<!ENTITY version "2.3">
]>
<doc>
Stargaze v2.3 is the most advanced astronomy product on the market.
</doc>
<?xml version='1.0'?><!DOCTYPE doc [
<!ENTITY product "Stargaze">
<!ENTITY version "2.3">
]>
<doc>
 Stargaze v2.3 is the most advanced astronomy product on the market.
</doc>
text_node = doc.root.children[0]
text_node.value = "&product; v&version; has a catalogue of 2.3 " +
                  "million celestial objects."
doc.write
<?xml version='1.0'?><!DOCTYPE doc [
<!ENTITY product "Stargaze">
<!ENTITY version "2.3">
]>
<doc>&product; v&version; has a catalogue of &version; million celestial objects.</doc>
<?xml version='1.0'?><!DOCTYPE doc [
<!ENTITY product "Stargaze">
<!ENTITY version "2.3">
]>
<doc>&product; v&version; has a catalogue of &version; million celestial objects.</doc>
text_node.raw = true
doc.write
<?xml version='1.0'?><!DOCTYPE doc [
<!ENTITY product "Stargaze">
<!ENTITY version "2.3">
]>
<doc>&product; v&version; has a catalogue of 2.3 million celestial objects.</doc>
<?xml version='1.0'?><!DOCTYPE doc [
<!ENTITY product "Stargaze">
<!ENTITY version "2.3">
]>
<doc>&product; v&version; has a catalogue of 2.3 million celestial objects.</doc>
text_node.value
"Stargaze v2.3 has a catalogue of 2.3 million celestial objects." "Stargaze v2.3 has a catalogue of 2.3 million celestial objects."
text_node.to_s
"&product; v&version; has a catalogue of 2.3 million celestial objects." "&product; v&version; has a catalogue of 2.3 million celestial objects."
str = %{
  <!DOCTYPE doc [ <!ENTITY year '2006'> ]>
  <doc>&copy; &year; Komodo Dragon &amp; Bob Productions</doc>
}
doc = REXML::Document.new str
text_node = doc.root.children[0]
text_node.value
"&copy; 2006 Komodo Dragon & Bob Productions" "&copy; 2006 Komodo Dragon & Bob Productions"
text_node.to_s
"&copy; &year; Komodo Dragon &amp; Bob Productions" "&copy; &year; Komodo Dragon &amp; Bob Productions"