我目前在读取 XHTML 时遇到问题,因为 XML 解析器无法识别 HTML 字符实体,因此:
<?php
$text = <<<EOF
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Entities are Causing Me Problems</title>
</head>
<body>
<p>Copyright © 2010 Some Bloke</p>
</body>
</html>
EOF;
$imp = new DOMImplementation ();
$html5 = $imp->createDocumentType ('html', '', '');
$doc = $imp->createDocument ('http://www.w3.org/1999/xhtml', 'html', $html5);
$doc->loadXML ($text);
header ('Content-Type: application/xhtml+xml; charset: utf-8');
echo $doc->saveXML ();
结果是:
Warning: DOMDocument::loadXML() [domdocument.loadxml]: Entity 'copy' not defined in Entity, line: 8 in testing.php on line 19
如何解决这个问题,同时允许自己以 XHTML5 形式提供页面?
XHTML5 没有 DTD,因此您不能在其中使用老式的 HTML 命名实体,因为没有文档类型定义来告诉解析器该语言的命名实体是什么。 (除了预定义的 XML 实体<
, &
, "
and >
... and '
,尽管您通常不想使用它)。
而是使用数字字符引用 (©
)或者更好的是,只是一个简单的未编码的©
字符(UTF-8 格式;记住包含<meta>
元素来表示非 XML 解析器的字符集)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)