【问题标题】:How to prevent Nokogiri from escape sequence replacement?如何防止 Nokogiri 进行转义序列替换?
【发布时间】:2019-08-08 13:08:45
【问题描述】:

我有一个像这样的文本节点:

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

当我尝试用这个打开和编辑文档时:

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

我在实际文档中得到了这个:

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

有没有办法保存&amp;quot; ?? 预期结果:

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

【问题讨论】:

标签: ruby xml nokogiri


【解决方案1】:

你可以试试这个方法:

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)

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2011-03-12
  • 1970-01-01
  • 2020-02-07
  • 1970-01-01
  • 2010-11-06
  • 2021-05-22
  • 2014-02-04
  • 1970-01-01
相关资源
最近更新 更多