html - 如何使用 Nokogiri::XML::Builder 将非转义符号添加到 HTML

标签 html xml utf-8 escaping nokogiri

我想使用 Nokogiri 中的 XML 生成器向 HTML 添加项目符号“•”之类的内容,但所有内容都被转义了。如何防止它被转义?

我希望结果是:

<span>&#8226;</span> 

而不是:

<span>&amp;#8226;</span> 

我只是这样做:

xml.span { 
  xml.text "&#8226;\ " 
}

我错过了什么?

最佳答案

如果你定义

  class Nokogiri::XML::Builder
    def entity(code)
      doc = Nokogiri::XML("<?xml version='1.0'?><root>&##{code};</root>")
      insert(doc.root.children.first)
    end
  end

然后这个

  builder = Nokogiri::XML::Builder.new do |xml|
    xml.span {
      xml.text "I can has "
      xml.entity 8665
      xml.text " entity?"
    }
  end
  puts builder.to_xml

产量

<?xml version="1.0"?>
<span>I can has &#x2022; entity?</span>

PS 这只是一个解决方法,要获得干净的解决方案,请参阅 libxml2 文档(Nokogiri 建立在 libxml2 上)以获得更多帮助。然而,即使是这些人admit that handling entities can be quite ..err, cumbersome sometimes .

关于html - 如何使用 Nokogiri::XML::Builder 将非转义符号添加到 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1811856/

相关文章:

javascript - 在 niceScroll Js 中禁用垂直滚动

html - 如何使用 css 在框中定位文本?

xml - 将html实体转换为utf-8字符

python - 在 OpenERP ver 7 中覆盖具有多个返回的创建方法

ruby-on-rails - Rails 3 - 如何处理 PG 错误不完整的多字节字符

python - 在python中将阿拉伯语unicode转换为字符串存储在mysql中并在lua中读取数据

c++ - 如何判断 Windows 剪贴板上的文本是 C++ 中的 ISO 8859 还是 UTF-8?

html - 如何在盒子/容器中水平和垂直居中对齐文本?

jquery - 如何自定义复选框,添加背景图片

javascript - 从属性中提取字符串