考虑以下代码:
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
已创建的属性。
- 谁对此负责,Nokogiri 还是 libxml2?
- 为什么会这样? (这是在执行标准吗?)
我能找到的最接近的是 this spec描述您可能如何将id
和name
具有相同值的属性。 - 有什么办法可以避免这种情况,因为希望使用
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 typeID
per element. Therefore, in XHTML 1.0 theid
attribute is defined to be of typeID
. In order to ensure that XHTML 1.0 documents are well-structured XML documents, XHTML 1.0 documents MUST use theid
attribute when defining fragment identifiers on the elements listed above.
[...]
Note that in XHTML 1.0, thename
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/