ruby - 为什么 Nokogiri 的 to_xhtml 从 `id` 创建新的 `name` 属性?

标签 ruby xhtml nokogiri libxml2

考虑以下代码:

require 'nokogiri' # v1.5.2
doc = Nokogiri.XML('<body><a name="foo">ick</a></body>')

puts doc.to_html
#=> <body><a name="foo">ick</a></body>

puts doc.to_xml
#=> <?xml version="1.0"?>
#=> <body>
#=>   <a name="foo">ick</a>
#=> </body>

 puts doc.to_xhtml
 #=> <body>
 #=>   <a name="foo" id="foo">ick</a>
 #=> </body>

注意新的 id已创建的属性。

  1. 谁对此负责,Nokogiri 还是 libxml2?
  2. 为什么会这样? (这是在执行标准吗?)
    我能找到的最接近的是 this spec描述您可能如何将 idname具有相同值的属性。
  3. 有什么办法可以避免这种情况,因为希望使用 to_xhtml输入的方法可能有 <a name="foo">

出现这个问题是因为我有一些输入正在用 id 解析一个元素和一个带有 name 的单独元素的属性发生冲突的属性。

最佳答案

显然 it's a feature libxml2.在 http://www.w3.org/TR/xhtml1/#h-4.10我们发现:

In XML, fragment identifiers are of type ID, and there can only be a single attribute of type ID per element. Therefore, in XHTML 1.0 the id attribute is defined to be of type ID. In order to ensure that XHTML 1.0 documents are well-structured XML documents, XHTML 1.0 documents MUST use the id attribute when defining fragment identifiers on the elements listed above.
[...]
Note that in XHTML 1.0, the name attribute of these elements is formally deprecated, and will be removed in a subsequent version of XHTML.

我想到的最好的“解决方法”是:

# Destroy all <a name="..."> elements, replacing with children
# if another element with a conflicting id already exists in the document
doc.xpath('//a[@name][not(@id)][not(@href)]').each do |a|
  a.replace(a.children) if doc.at_css("##{a['name']}")
end

关于ruby - 为什么 Nokogiri 的 to_xhtml 从 `id` 创建新的 `name` 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10252663/

相关文章:

ruby - 解释 1<<16 | 10?

尝试隐藏表格行时出现 JavaScript 错误

css - 高度 :100% margin white space/block problem

Ruby gem Nokogiri 1.9.1 在具有不同 LibXML 版本的 Mac 上失败

ruby-on-rails - Nokogiri XML 和循环不起作用

arrays - 如何在 Ruby 中添加数组中的元素

ruby - 请解释这个 ruby 纤维示例背后的逻辑

ruby-on-rails - 请务必在 RoR 中安装时区支持 - https ://github. com/ankane/groupdate#for-mysql

jquery - 从不同元素调用 jQuery 函数

ruby-on-rails - 使用 Nokogiri 添加节点