gpt4 book ai didi

ruby - nokogiri 可以在保存 xml 时对属性使用单引号吗?

转载 作者:太空宇宙 更新时间:2023-11-03 16:10:35 25 4
gpt4 key购买 nike

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

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

REXML 有这个用于设置单引号/双引号:

doc.context[:attribute_quote] = :quote 

我找不到与 nokogiri 类似的内容。

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

最佳答案

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

  • 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,如下所示:

&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 serialize single quote attributes不会立即打开任何确凿的证据。

我认为你应该file a feature request看看你能得到什么样的温柔。 :)

关于ruby - nokogiri 可以在保存 xml 时对属性使用单引号吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4822922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com