DOMDocument :: loadXML与HTML实体

cas*_*asr 6 php xml html5 entities domdocument

我目前在使用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 &copy; 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 ();
Run Code Online (Sandbox Code Playgroud)

结果是:

Warning: DOMDocument::loadXML() [domdocument.loadxml]: Entity 'copy' not defined in Entity, line: 8 in testing.php on line 19

如何在允许自己将页面作为XHTML5提供的同时解决这个问题?

bob*_*nce 12

XHTML5没有DTD,所以你可能不使用老派的HTML命名实体在里面,因为没有文档类型定义告诉解析器什么命名实体是这种语言.(除预定义XML实体&lt;,&amp;,&quot;&gt;...和&apos;,虽然你一般不希望使用).

改用数字字符引用(&#169;),或者,更好的,只是一个普通的未编码的©字符(在UTF-8;记住包括<meta>以表示的字符集的非XML解析器元素).