本文介绍了nokogiri 可以对保存 xml 的属性使用单引号吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Nokogiri 使用双引号保存 XML 属性,即使它读取的 DOM 有单引号.这当然是完全合法的,但是即使 DOM 没有更改,它也会对文件内容进行烦人的更改,使 diff 和 git 等工具更难弄清楚发生了什么.

Nokogiri saves XML attributes with double quotes, even if DOM it read had single quotes.This is totally legitimate of course, but it introduces annoying changes to file contents even when DOM didn't change, making it harder for tools like diff and git to figure out what happened.

我可以强制它使用单引号吗(或者甚至更好,如果没有任何修改,保留原始中的任何引用样式)?

Can I force it to use single quotes (or even better, keep whatever quoting style there was in the original if nothing was modified)?

REXML 用于设置单/双引号:

REXML has this for setting single/double quote:

doc.context[:attribute_quote] = :quote 

我找不到与 nokogiri 相似的内容.

I couldn't find anything similar for nokogiri.

是否可以用单引号保存文档?

Is it possible to have it save documents with single quotes?

推荐答案

看起来答案是否定的;不像图书馆目前编写的那样,也许根本不是.跟踪节点序列化的调用路径:

It looks like the answer is no; not as the library is currently written, and maybe not at all. Tracing the call path for a node's serialization:

  • Nokogiri::XML::Node#to_s 调用 to_xml
  • Nokogiri::XML::Node#to_xml 调用 serialize (设置一些默认选项)
  • Nokogiri::XML::Node#serialize 调用 write_to
  • Nokogiri::XML::Node#write_to 调用 native_write_to
  • Nokogiri::XML::Node#native_write_to 调用 native_write_to,如下所示:
  • Nokogiri::XML::Node#to_s calls to_xml
  • Nokogiri::XML::Node#to_xml calls serialize (sets a few default options)
  • Nokogiri::XML::Node#serialize calls write_to
  • Nokogiri::XML::Node#write_to calls native_write_to
  • Nokogiri::XML::Node#native_write_to calls native_write_to, which looks like this:

&bsp;

def native_write_to(io, encoding, indent_string, options)
  set_xml_indent_tree_output 1
  set_xml_tree_indent_string indent_string
  savectx = LibXML.xmlSaveToIO(IoCallbacks.writer(io), nil, nil, encoding, options)
  LibXML.xmlSaveTree(savectx, cstruct)
  LibXML.xmlSaveClose(savectx)
  io
end

因此,此时您受制于 libxml.谷歌搜索 libxml 序列化单引号属性 不会立即出现任何吸烟枪.

So, you are at the mercy of libxml at this point. Googling for libxml serialize single quote attributes does not immediately turn up any smoking guns.

我认为你应该提交功能请求,看看你能得到什么样的温柔.:)

I think you should file a feature request and see what sort of tenderlovin' you can get. :)

这篇关于nokogiri 可以对保存 xml 的属性使用单引号吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 14:16