Ruby 如何防止Nokogiri替换逃逸序列?

Ruby 如何防止Nokogiri替换逃逸序列?,ruby,xml,nokogiri,Ruby,Xml,Nokogiri,我有这样一个文本节点: <string>Lorem Ipsum - All the &quot;facts&quot;.</string> 我在实际文件中得到了这一点: <string>Lorem Ipsum - All the "facts".</string> 您可以尝试以下方法: require 'nokogiri' doc_name = 'strings.xml' output_file_name = "out.xml"

我有这样一个文本节点:

<string>Lorem Ipsum - All the &quot;facts&quot;.</string>
我在实际文件中得到了这一点:

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

您可以尝试以下方法:

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)

它只是转义标记内的所有文本,但保持标记不变。

您可以尝试以下方法:

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)

它只是转义标记内的所有文本,但保持标记不变。

可能重复不幸的是它不起作用可能重复不幸的是它不起作用更改为“在我的情况下需要保存”哦,这可能是个问题它更改为“在我的情况下需要保存”哦,这可能是个问题
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)