字符实体引用 - 数字或非数字?
所以,我知道我可以将&符号表示为 &
或 &
。
我发现至少一种解析 XML 的方法不允许基于缩写的样式 - 仅限数字。有最佳实践吗?由于我的经验,我想指导我的团队使用数字版本,但一个例子似乎不足以说服他们。
我们应该选择哪种方法?
So, I know that I can represent an ampersand as &
or &
.
I have found that at least one method of parsing XML does not allow for the abbreviation-based style - only numeric. Is there a best-practice? I want to instruct my team to use the numeric versions because of my experience, but one instance hardly seems like enough reason to convince them.
Which method should we favor?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
XML 仅具有一小部分这些符号实体,例如 amp, quot, gt和lt。
我们熟悉的符号名称 © 等实体因其出现在 HTML DTD 中而存在,此处 http://www.w3.org/TR/html4/sgml/entities.html(尽管我认为大多数浏览器都内置了此功能)。
因此,如果您使用 (X)HTML,请确保您的文档类型正确,然后按照 w3.org 上的 XHTML 链接查看可用的实体。
就最佳实践而言,大多数人发现符号名称更容易理解,并且会在可用时使用它们。我会推荐这个。
不这样做的唯一原因是,在非常旧的浏览器中曾经存在实体无法工作的情况 - 但我不相信这种情况已经发生了。
XML only has a small set of these symbolic entities, for amp, quot, gt and lt.
The symbolic names we're familiar with for ©, etc. for entities exist because of their appearance in the HTML DTD, here http://www.w3.org/TR/html4/sgml/entities.html (although I think most browsers have this baked in).
Therefore, if you are using (X)HTML, get your doctype right, and then follow the links on w3.org to XHTML to see the entities available.
As far as best practices, most people find the symbolic names easier to understand and will use them when available. I would recommend that.
The only reason not to is that there used to be cases in very old browsers when entities wouldn't work-- but I don't believe this is the case any more.
如果您指的是其他 HTML 实体,则使用纯 XML,则仅实体 amp、lt、gt、quot、和 apos 是预定义的(apos 在 HTML 中不可用,但 amp 确实应该如此)。
但是,除非在 DOCTYPE 中定义,否则所有其他 HTML 实体(例如 nbsp)将不可用,因此在这种情况下,使用数字实体可能确实更好。
If you mean other HTML entities, with pure XML, only the entities amp, lt, gt, quot, and apos are pre-defined (apos is not available in HTML, but amp indeed should be).
However, all other HTML entities (such as nbsp) will not be available unless defined in the DOCTYPE, so in such a case, using numeric entities may indeed be preferable.