Reputation: 1632
Is there a way to edit the text of a nokogiri element? I have a nokogiri element that contains a list element (<li>
) and I would like to remove some characters from the text while preserving the <li>
html. Specifically, I want to remove a leading ":" character in the text if it exists. It doesn't look like there's a text= method for nokogiri elements, but I just wanted to make sure.
Maybe I will have to use regular expressions? If so, how would I remove a leading ":" if it looks something like:
<li>: blah blah blah</li>
p.s. I am using ruby.
Upvotes: 3
Views: 2439
Reputation: 108039
#!/usr/bin/ruby1.8
require 'rubygems'
require 'nokogiri'
html = <<EOS
<ul>
<li>: blah blah blah</li>
<li>: foo bar baz</li>
</ul>
EOS
doc = Nokogiri::HTML.parse(html)
for li in doc.xpath('//li/text()')
li.content = li.content.gsub(/^: */, '')
end
puts doc.to_html
# => <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
# => <html><body><ul>
# => <li>blah blah blah</li>
# => <li>foo bar baz</li>
# => </ul></body></html>
Upvotes: 5