(某些)HTML 实体破坏 XSL 引擎是否正常?
我有一个用 PHP DOMDocument 动态生成的 XML 文档。我将此 XML 与 XSL 文件一起使用。它工作得很好,直到我从数据库条目中获取日元货币符号 (¥)。该符号被转义为 ¥
HTML 实体。该实体正在破坏 XSL/XML 引擎:
警告:DOMDocument::load() [domdocument.load]:实体“日元”不是 在 %file.xml% 中定义,第 %1% 行,位于 %file.php% 的第 %2% 行
当 ¥
实体不存在时,一切正常。
是否有一些我尚未完成/包含/配置的操作会出现此错误?
I have a dynamically generated XML document made with PHP DOMDocument. I use this XML with an XSL file. It worked fine until I got the Yen currency symbol (¥) from a database entry. This symbol is escaped to the ¥
HTML entity. This entity is breaking the XSL/XML engine:
Warning: DOMDocument::load() [domdocument.load]: Entity 'yen' not
defined in %file.xml%, line: %1% in %file.php% on line %2%
When the ¥
entity is not there everything works well.
Is there something I haven't done/included/configured to get this error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
XML 只识别少数字符实体:
<
、>
"
、&
、'
。除非您自己添加实体的定义,否则任何其他内容都将是解析错误:XML only recognizes a few character entities:
<
,>
"
,&
,'
. Anything else would be a parsing error unless you add in definitions for the entities yourself:<!ENTITY yen "¥">
使用
¥
您的 XML 就变成了格式不正确的 XML,因为 XML 没有预定义的¥
实体。¥
的有效转义版本是¥
。With
¥
your XML becomes not well-formed XML, because XML doesn't have predefined¥
entity. Valid escape version of¥
is¥
.在使用 XML 时,默认定义的唯一实体是
&
、<
和>.任何其他实体都需要在 DTD 中定义。
XSL 在其文档类型中没有定义任何其他实体。即使您要转换为 (x)HTML,您也没有定义其他实体,因为您的主要文档类型适用于 XSL。
您可以添加一大堆声明来在 DTD 中定义它们,但这意味着使用自定义 DTD,这并不理想,而且需要大量维护工作。
坦率地说,最简单的解决方案就是对所有内容使用数字实体代码。这很痛苦,但它可以在任何 XML 方言中工作,而不需要对 DTD 进行任何修改。
When it comes to working with XML, the only entities that are defined by default are
&
,<
and>
. Any other entities need to be defined in the DTD.XSL does not define any other entities in its doctype. Even if you're converting to (x)HTML, you don't have those other entities defined because your primary doctype is for XSL.
You could add a big bunch of declarations to define them in the DTD, but this would mean using a custom DTD, which isn't ideal, and is also a lot of work to maintain.
Frankly, the easiest solution to this is simply to use numeric entity code for everything. It's a pain, but it works in any XML dialect without needing any hacking with the DTD.