如何将 HTML 实体包含到 XML 文件中 [英] How to include HTML entities into an XML file

查看:48
本文介绍了如何将 HTML 实体包含到 XML 文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在火狐:

<?xml version="1.0" encoding="utf-8"?>
<math display="block" xmlns="http://www.w3.org/1998/Math/MathML">
  <mi>&rho;</mi>
</math>

导致未定义实体"错误.

results in "undefined entity" error.

我知道那里缺少一些东西.我只是不知道我应该写什么来纠正这个问题.我想避免将每个 unicode 字符都重写到文档中.

I know there is something missing there. I just don't know what I should write to correct the problem. I would like to avoid rewriting every single unicode character into the document.

编辑我尝试了以下操作,仍然无法正常工作,同样的错误:

EDIT I tried the following, still not working, same error :

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE math [
  <!ENTITY % HTMLlat1 PUBLIC
    "-//W3C//ENTITIES Latin 1 for XHTML//EN"
    "xhtml-lat1.ent">
  %HTMLlat1;
  <!ENTITY % HTMLsymbol PUBLIC
    "-//W3C//ENTITIES Symbols for XHTML//EN"
    "xhtml-symbol.ent">
  %HTMLsymbol;
  <!ENTITY % HTMLspecial PUBLIC
    "-//W3C//ENTITIES Special for XHTML//EN"
    "xhtml-special.ent">
  %HTMLspecial;
]>
<math display="block" xmlns="http://www.w3.org/1998/Math/MathML">
  <mi>&rho;</mi>
</math>

EDIT 在 chrome 中,这会导致以下消息:

EDIT In chrome, this results in the following message :

error on line 6 at column 13: PEReference: %HTMLlat1; not found
warning on line 10 at column 15: PEReference: %HTMLsymbol; not found
warning on line 14 at column 16: PEReference: %HTMLspecial; not found

EDIT 尝试下载 .ent 文件并将引用更改为本地 http://路径或 file:///路径,但未成功.关于该主题的类似帖子:PHP 中的 XML 目录

EDIT Tried to download the .ent files and change the reference to either a local http:// path or file:/// path with no success. A similar post about the subject : XML catalog in PHP

编辑浏览器的快速解决方法:

EDIT Quick workaround for browsers :

<!DOCTYPE html>
<math display="block" xmlns="http://www.w3.org/1998/Math/MathML">
  <mi>&rho;</mi>
</math>

您需要抑制 XML 标头,因此将其理解为 HTML.

You need to suppress the XML header, so it is understood as HTML.

尽管如此,这并没有回答问题,因为问题是导入实体,而文档被声明为 XML.

Nevertheless, this doesn't answer the question, as the question was to import entities, while the document is declared as XML.

答案

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE math PUBLIC "-//W3C//DTD MathML 2.0//EN" "http://www.w3.org/Math/DTD/mathml2/mathml2.dtd">
<math display="block" xmlns="http://www.w3.org/1998/Math/MathML">
  <mi>&rho;</mi>
</math>

推荐答案

在 XML 声明之后添加 MathML 2.0 文档类型:

Add the MathML 2.0 doctype, after the XML declaration:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE math
    PUBLIC "-//W3C//DTD MathML 2.0//EN"
           "http://www.w3.org/Math/DTD/mathml2/mathml2.dtd"
>

原因是实体引用的处理在 Web 浏览器中非常笨拙.他们实际上并不读取 DTD.相反,它们具有预定义实体的内置表,可以使用特定的 doctype 字符串打开这些表.这是字符串魔术,例如使用 MathML 3.0 doctype 将不起作用.参见到 XML使用 XSLT 转换为 XHTML:使用诸如 &Sum; 之类的实体;(这是一个 MATHML 实体)(尤其是 Martin Honnen 对答案的评论).

The reason is that handling of entity references is very kludgy in web browsers. They do not actually read DTDs. Instead, they have built-in tables of predefined entities, which can be turned on by using specific doctype strings. This is string magic, and e.g. using MathML 3.0 doctype will not work. Cf. to XML to XHTML using XSLT: using entities such as &Sum; (which is a MATHML entity) (especially Martin Honnen’s comment on an answer).

或者,使用字符本身,或者,如果您的创作系统无法方便地生成它们,则使用字符引用,例如 &#x3c1;.

Alternatively, use characters as such or, if your authoring system cannot produce them conveniently, character references like &#x3c1;.

这篇关于如何将 HTML 实体包含到 XML 文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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