扩展 XHTML DTD 以在 ID 属性中使用特殊字符
我想验证作为 XHTML 扩展的 XML 模板。现在,ID 属性中出现了特殊字符,例如 {
和 |
。是否可以扩展 XHTML DTD 以覆盖对 ID 属性中允许的字符的限制?或者是XML规范定义的字符?
I want to validate XML templates that are a XHTML extension. Now there are special characters like {
and |
in ID attributes. Is it possible to extend the XHTML DTD to overwrite the restriction to the characters allowed in the ID attribute? Or are the characters defined by the XML specification?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不能使用字符“{”和“|”直接在 id 属性中,因为在 XML 规范 中它说
名称生成位于此处。如果展开语法规则,您会发现名称中允许的唯一字符由这些产生式给出:
不幸的是,不允许使用左括号和管道。这些字符的代码点分别是#7B 和#7C;不在可接受的字符范围内。
TL;DR:ID 属性的合法字符由 XML 规范拥有,并且您的两个字符不合法。
附录
以下是一些示例。以下文档通过了 W3C 验证站点上的 XHTML 验证:
但以下文档不会通过
我们收到错误:
现在非常有趣的是,如果您确实想要在 id 名称中使用左大括号,您可以尝试 这个:
但是你得到了同样的错误!你可能想尝试一下这个;验证器显示带有与号散列 x 7 b 分号的行,但它认为那里有一个左大括号。
最重要的是,您不能使用 XML 规范允许的字符以外的字符。
You cannot use the characters '{' and '|' directly in id attributes because in the XML specification it says
The name production is here. If you expand the syntax rule you see that the only characters allowed in a name are given by these productions:
Unfortunately the left brace and the pipe are not allowed. The codepoints for those characters are #7B and #7C respectively; not in the accepted character ranges.
TL;DR: the legal characters for ID attributes are owned by the XML spec and your two characters are not legal.
ADDENDUM
Here are some examples. The following document passes validation for XHTML on the W3C validation site:
but the following will not
We get the error:
Now it's rather interesting that if you really want the left curly bracket in the id name, you can try this:
But you get the same error! You might want to try this; the validator shows the line with the ampersand hash x seven b semicolon but it thinks there is a left brace there.
The bottom line is that you simply cannot have ids with characters other than those allowed by the XML specification.