为了补充另一个示例,以下是如何创建一个具有 head、title 和 body 元素的 XHTML 1.0 Transitional 文档。
<?php
$document = DOMImplementation::createDocument(null, 'html',
DOMImplementation::createDocumentType("html",
"-//W3C//DTD XHTML 1.0 Transitional//EN",
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"));
$document->formatOutput = true;
$html = $document->documentElement;
$head = $document->createElement('head');
$title = $document->createElement('title');
$text = $document->createTextNode('Title of Page');
$body = $document->createElement('body');
$title->appendChild($text);
$head->appendChild($title);
$html->appendChild($head);
$html->appendChild($body);
echo $document->saveXML();
?>
这将输出: (http 链接已删除,因为是垃圾邮件)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "doctype.dtd">
<html xmlns="w3org1999xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Title of Page</title>
</head>
<body></body>
</html>
请注意 saveXML 函数。如果使用 saveHTML 函数代替,则会得到以下输出
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "doctype.dtd">
<html>
<head><title>Title of Page</title></head>
<body></body>
</html>