2

I have a text node like so:

<string>Lorem Ipsum - All the &quot;facts&quot;.</string>

When I try to open and edit the document with this:

doc = Nokogiri::XML(File.open(doc_name))
doc.search("//string[text() = '...']").remove

I get this in the actual document:

<string>Lorem Ipsum - All the "facts".</string>

Is there a way to save &quot; ?? Expected result:

<string>Lorem Ipsum - All the &quot;facts&quot;.</string>
Eric Russo
  • 55
  • 5

1 Answers1

0

You can try this approach:

require 'nokogiri'
doc_name = 'strings.xml'
output_file_name = "out.xml"
doc = Nokogiri::XML(File.open(doc_name))
doc.search("//string[text() = '...']").remove
doc.search('//text()').each { |node| node.content = CGI.escape_html(node.text) }
end
File.write(output_file_name, doc.to_xml)

It simply escapes all text inside tags but keeps tags untouched.

Pavel Oganesyan
  • 6,774
  • 4
  • 46
  • 84