在HTML文档中使用自定义实体 [英] Using custom entities in HTML documents

查看:90
本文介绍了在HTML文档中使用自定义实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

tl; dr



我想了解是否有可能在HTML文档中使用自定义实体进行本地化(以及支持情况如何)






我想象的是这样做的:

 <!DOCTYPE html/locales/en-us.ent\"> 
< html>
< head>
< title>& contactus.title;< / title>
< / head>
< body>
< p>& contactus.youcanreach;< br> 123,范例道路< br> 12345范例城市< / p>
< ul id =menu>
< li>& menu.home;< / li>
< li>& menu.products;< / li>
< li>& menu.contactus;< / li>
< / ul>
< / body>
< / html>

并且所有实体都将存储在一个文件中(每种语言一个,en-us.ent例如上面的例子),例如

 <!ENTITY menu.homeHome> ; 
<!ENTITY menu.products产品>
<!ENTITY menu.contactus与我们联系>
...

最终,这甚至可以用于HTML片段(不确定是否这样(如标题,菜单等);在上面的例子中,整个< ul> 可以是这样一个片段)



现在,我的理解是这在XHTML中是理论上可行的,但我想知道这是否也可以在HTML中完成,并且如果浏览器和爬虫)将会应付。

解决方案

理论上,这是可能的。 HTML 4.x(以及之前的几个版本)都是SGML应用程序,因此您可以使用新实体扩展DTD。实际上,每个主流浏览器都实现了HTML特定的标签汤而不是一个真正的SGML解析器,所以你不能这样做。这就是为什么HTML 4有一个 SGML功能列表, a>以及为什么HTML 5不是SGML应用程序。


tl;dr

I'd like to understand if it is possible (and, in case, how well supported) to use custom entities in HTML documents for localization purposes.


What I envision is doing something like this:

<!DOCTYPE html "/locales/en-us.ent">
<html>
  <head>
    <title>&contactus.title;</title>
  </head>
  <body>
    <p>&contactus.youcanreach;<br>123, Example Road<br>12345 Example City</p>
    <ul id="menu">
      <li>&menu.home;</li>
      <li>&menu.products;</li>
      <li>&menu.contactus;</li>
    </ul>
  </body>
</html>

and all entities would be stored in a file (one for each language, en-us.ent in the example above) that gets included at the top of the document, e.g.

<!ENTITY menu.home "Home">
<!ENTITY menu.products "Products">
<!ENTITY menu.contactus "Contact us">
...

Eventually this could even be exapnded to HTML fragments (not sure if this is really allowed) that may be useful on all pages (such as headers, menus, etc.; in the example above, the whole <ul> could be such a fragment)

Now, my understanding is that this is theoretically possible in XHTML, but I was wondering if this can be done also in HTML and, in case, how well browsers (and crawlers) would cope.

解决方案

In theory, it is possible. HTML 4.x (and several previous versions) are SGML applications so you can extend the DTD with new entities.

In practise, every mainstream browser implements an HTML specific tag soup slurper instead of a real SGML parser so you can't do this. This is why HTML 4 has a list of SGML features to avoid and why HTML 5 isn't an SGML application.

这篇关于在HTML文档中使用自定义实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆