Xhtml 无效字符?
我在 .NET 中制作了自定义 xhtml valdidator(通过 dtd + 一些额外规则进行验证),并且我注意到我的验证和 w3c 验证之间存在差异。
在我的验证器中,当 id 中存在冒号时,我会收到以下错误(比方说: id="mustang:horse")
(错误)根据其数据类型,“id”属性具有无效值。
但我在 w3c 上没有收到此模式的任何错误。
我尝试在 xml/xhtml 中查找属性的无效字符列表,但找不到?
感谢您的帮助,
I have made custom xhtml valdidator in .NET(validating through dtd + some extra rules) and I have noticed a discrepancy between my validation and w3c validation.
In my validator I get the following error when there is colon in the id (let's say : id="mustang:horse")
(Error) The 'id' attribute has an invalid value according to its data type.
But I do not get any errors on w3c for this pattern.
I tried to find a list of invalid characters for an attribute in xml/xhtml but couldn't find it?
Thank you for your help,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一个列表,并且它确实允许使用冒号。
XHTML 1.0 规范位于 http://www.w3.org/TR/xhtml1/ #h-4.10
XML 1.0 规范在 http://www.w3.org/TR/2008/REC-xml-20081126/#id
名称生成定义于 http://www.w3 .org/TR/2008/REC-xml-20081126/#NT-Name
上面还提到了这个正式定义:
(我的重点)
There is a list and and it does permit colons.
The XHTML 1.0 spec says at http://www.w3.org/TR/xhtml1/#h-4.10
The XML 1.0 spec says at http://www.w3.org/TR/2008/REC-xml-20081126/#id
And the Name production is defined at http://www.w3.org/TR/2008/REC-xml-20081126/#NT-Name
And also says above this formal definition:
(My emphasis)
造成这种差异的原因是 W3C 验证器似乎没有进行命名空间感知的 XHTML 处理。虽然 XHTML 文档需要位于 XHTML 命名空间中,但这实际上是合理的,因为 HTML 文档不使用命名空间,并且 XHTML 文档(如 HTML)的规范有效结构是由 DTD 文件定义的,而 DTD 实际上并不感知命名空间。
就像 @Alochi 已经指出的那样:
当文档被解析为不了解名称空间时,这是正确的,但如果文档需要与名称空间一致,则情况并非如此。 XML 规范中的命名空间规定 ID 必须与 NCName 生成相匹配禁止冒号字符。命名空间感知解析是一种常见约定,因此不建议在 id 值中使用冒号,尽管当文档解析不感知命名空间时允许使用冒号。
摘要:如果忽略命名空间,则
ID
值必须是有效的Name
并且可以包含冒号;否则它必须是有效的NCName
并且不能包含冒号。The reason for this difference is that W3C validator doesn't seem to do namespace aware XHTML processing. Although XHTML documents need to be in the XHTML namespace, this is actually reasonable, because HTML documents are not using namespaces and the normative valid structure of XHTML documents (as HTML) is defined by a DTD file and DTDs are not actually namespace aware.
Like @Alochi already noted:
This is true when the document is parsed as not namespace aware, but it is not true if the document needs to be namespace conformant. The Namespaces in XML specification states that IDs must match NCName production which explicitly forbids the colon character. Namespace aware parsing is a common convention and therefore using a colon in the value of a id is not recommended even though it is allowed when the document parsing is not namespace aware .
Summary: if namespaces are ignored, an
ID
value must be a validName
and it can contain a colon; otherwise it must be a validNCName
and it can't contain a colon.