如何在有效的 XML 文档中定义 HTML 实体引用?

发布于 2024-11-17 13:31:05 字数 661 浏览 1 评论 0原文

我需要能够在 XML 文档中引用命名的 HTML 实体,例如 •,而不是 Unicode 替代 •。我可以控制 XML 文档的某些部分,例如定义 DOCTYPE,但在实际 XML 中执行查找和替换不是一个选项。我可以通过包含 XHTML 过渡 DOCTYPE 来获取一些元素,例如  &,但我需要更多手动定义。我该怎么做?

-- 编辑 --

感谢吉姆的回答,这就是我的最终结果。这很棒,因为我可以利用 XHTML 过渡实体,还可以添加我自己的:

<!DOCTYPE
   html
   PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
   [
      <!ENTITY bull  "&#8226;">
      <!ENTITY ldquo "&#8220;">
      <!ENTITY rdquo "&#8221;">
      ... etc ...
   ]
>

I need to be able to reference named HTML entities like instead of the Unicode alternative in an XML document. I have control over some parts of the XML document, such as defining the DOCTYPE, but doing a find-and-replace in the actual XML is not an option. I can get some elements like   and & by including the XHTML transitional DOCTYPE, but I need to define more manually. How do I do this?

-- EDIT --

Thanks to Jim's answer, here's what I ended up with. This is great because I can utilize the XHTML transitional entities, and also add my own:

<!DOCTYPE
   html
   PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
   [
      <!ENTITY bull  "•">
      <!ENTITY ldquo "“">
      <!ENTITY rdquo "”">
      ... etc ...
   ]
>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

讽刺将军 2024-11-24 13:31:05

如果您可以修改 XML 以包含内联 DTD,则可以在其中定义实体:

<!DOCTYPE yourRootElement [
    <!ENTITY bull "•">
    ....
]>

If you can modify the the XML to include an inline DTD you can define the entities there:

<!DOCTYPE yourRootElement [
    <!ENTITY bull "•">
    ....
]>
故事未完 2024-11-24 13:31:05

我不确定,但我认为 XHTML DTD 应该为您提供相当多的实体 (253):

http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references#Entities_representing_special_characters_in_XHTML

另外,在 w3 规范中,还提到了特殊字符等的附加 DTD。
http://www.w3.org/TR/xhtml-modularization/dtd_module_defs .html#a_dtd_xhtml_character_entities

但是我还没有找到实现示例特殊字符 DTD。


由 DevNull 编辑

这是实体 DTD 模块之一的极其通用的示例实现。实现时只需要添加一个指向模块的参数实体即可。

<?xml version="1.0"?>
<!DOCTYPE test [
<!ELEMENT test (#PCDATA)>
<!ENTITY % xhtml-special SYSTEM "xhtml-special.ent">
%xhtml-special;
]>
<test>Here is a left double quote: “</test>

I'm not cetain, but I think the XHTML DTD's should give you quite a few entities (253):

http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references#Entities_representing_special_characters_in_XHTML

Also in the w3 spec, there is a mention of additional DTD's for special characters etc.
http://www.w3.org/TR/xhtml-modularization/dtd_module_defs.html#a_dtd_xhtml_character_entities

However I haven't been able to find an implementation example of the special character DTDs.


Edit by DevNull

Here is an extremely generic example implementation of one of the entity DTD modules. To implement, you only need to add a parameter entity pointing to the module.

<?xml version="1.0"?>
<!DOCTYPE test [
<!ELEMENT test (#PCDATA)>
<!ENTITY % xhtml-special SYSTEM "xhtml-special.ent">
%xhtml-special;
]>
<test>Here is a left double quote: “</test>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文