Here's what I'd do:
Parse some XML first:
require 'nokogiri'
doc = Nokogiri::XML(<<EOT)
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="nutrition.css"?>
<nutrition>
<daily-values>
<total-fat units="g">65</total-fat>
<saturated-fat units="g">20</saturated-fat>
<cholesterol units="mg">300</cholesterol>
<sodium units="mg">2400</sodium>
<carb units="g">300</carb>
<fiber units="g">25</fiber>
<protein units="g">50</protein>
</daily-values>
<food>
<name>Avocado Dip</name>
<mfr>Sunnydale</mfr>
<serving units="g">29</serving>
<calories total="110" fat="100"/>
<total-fat>11</total-fat>
<saturated-fat>3</saturated-fat>
<cholesterol>5</cholesterol>
<sodium>210</sodium>
<carb>2</carb>
<fiber>0</fiber>
<protein>1</protein>
<vitamins>
<a>0</a>
<c>0</c>
</vitamins>
<minerals>
<ca>0</ca>
<fe>0</fe>
</minerals>
</food>
</nutrition>
EOT
If I want to delete a node's content, I can remove its children
or assign nil to its content:
doc.at('total-fat').to_xml # => "<total-fat units=\"g\">65</total-fat>"
doc.at('total-fat').children.remove
doc.at('total-fat').to_xml # => "<total-fat units=\"g\"/>"
or:
doc.at('saturated-fat').to_xml # => "<saturated-fat units=\"g\">20</saturated-fat>"
doc.at('saturated-fat').content = nil
doc.at('saturated-fat').to_xml # => "<saturated-fat units=\"g\"/>"
If I want to extract the text from a node for use some other way:
food = doc.at('food').text
# => "\n Avocado Dip\n Sunnydale\n 29\n \n 11\n 3\n 5\n 210\n 2\n 0\n 1\n \n 0\n 0\n \n \n 0\n 0\n \n "
or:
food = doc.at('food').children.map(&:text)
# => ["\n ",
# "Avocado Dip",
# "\n ",
# "Sunnydale",
# "\n ",
# "29",
# "\n ",
# "",
# "\n ",
# "11",
# "\n ",
# "3",
# "\n ",
# "5",
# "\n ",
# "210",
# "\n ",
# "2",
# "\n ",
# "0",
# "\n ",
# "1",
# "\n ",
# "\n 0\n 0\n ",
# "\n ",
# "\n 0\n 0\n ",
# "\n "]
or however else you want to mangle the text.
And, if you want to mark that you've removed the text:
doc.at('food').content = 'REMOVED'
doc.at('food').to_xml # => "<food>REMOVED</food>"
You could also use an XML comment instead:
doc.at('food').children = '<!-- REMOVED -->'
doc.at('food').to_xml # => "<food>\n <!-- REMOVED -->\n</food>"
node.unlink
will remove it from a DOM. – Oospore