Core\Xml\Writer::writeDoctype
Writes a document type declaration, such as <!DOCTYPE html>, before the root element.
Some programs that read XML look at this line to learn what kind of document they get. An XHTML page is a common example: a browser reads <!DOCTYPE html> and shows the page in standards mode. The declaration names the document type, and usually that name is the name of the root element.
writeDoctype writes only the name. It does not write a link to a DTD file or any entity definitions, so the document does not depend on any other file. A document has at most one declaration, and it must come before the root element. In other cases, writeDoctype throws a LogicError and writes nothing.
The declaration is for programs outside Novis. Core\Xml::parse and Core\Xml::reader throw an error for any document that contains <!DOCTYPE.
The examples below write a declaration, show the errors, and write an XHTML page.
Parameters
$namestringThe name of the document type. It must be an XML name. Usually it is the name of the root element.
Return value
Returns void. Nothing. The declaration is written before the root element.
Errors
LogicErrorThe root element is already started. A declaration is already written. The name is not an XML name. The document is not open.
Changelog
| Version | Change |
|---|---|
0.0.6 | Every Core\Xml\Writer method that writes a part of the document now starts with write. For example, writeText writes text and writeAttribute writes an attribute. The old names were content, attribute, comment, cdata, instruction and doctype. |
Examples
Name the document type
<?nvs// Shows how to write a document type declaration, and that the Novis parser does not read it.
Core\Xml\Writer $writer = Core\Xml::writer({ indent: ' ' });$writer->startDocument();// The declaration comes before the root element. Its name is the name of the root element.$writer->writeDoctype('note');$writer->startElement('note');$writer->writeText('Call the supplier on Monday.');$writer->endElement();
string $document = $writer->endDocument();echo $document, "\n";
// `Core\Xml::parse` throws an error for a document with a declaration.// This prints the error message.try { Core\Xml::parse($document);} catch (ParseError $error) { echo $error->message, "\n";}<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE note><note>Call the supplier on Monday.</note>Core\Xml::parse(): a document type declaration is not read at all — no entity is resolved from an external subset, from an internal subset or from the network, so there is nothing a `<!DOCTYPE …>` could mean here.When a declaration is not allowed
<?nvs// Shows the errors `writeDoctype` throws, and that a failed call writes nothing.
Core\Xml\Writer $writer = Core\Xml::writer();$writer->startDocument();
// A name cannot start with a digit. This prints the error message.try { $writer->writeDoctype('2024-report');} catch (LogicError $error) { echo $error->message, "\n";}
$writer->writeDoctype('report');
// A document has only one declaration. This prints the error message.try { $writer->writeDoctype('report');} catch (LogicError $error) { echo $error->message, "\n";}
$writer->startElement('report');
// The root element is already started, so it is too late. This prints the error message.try { $writer->writeDoctype('report');} catch (LogicError $error) { echo $error->message, "\n";}
// The document has one declaration and the root element.$writer->endElement();echo $writer->endDocument(), "\n";Core\Xml\Writer::writeDoctype(): `2024-report` is not a name a document can write.Core\Xml\Writer::writeDoctype(): a document has at most one document type declaration, and one is already written.Core\Xml\Writer::writeDoctype(): a document type declaration goes above the root element, and the root element is already written.<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE report><report/>An xhtml page
<?nvs// Shows how to write an XHTML page that a browser shows in standards mode.
array<string> $items = ['Green tea', 'Black tea', 'Herbal tea'];
Core\Xml\Writer $writer = Core\Xml::writer({ indent: ' ' });$writer->startDocument();// A browser reads `<!DOCTYPE html>` and uses its standards mode for the page.$writer->writeDoctype('html');$writer->startElement('html');$writer->writeAttribute('xmlns', 'http://www.w3.org/1999/xhtml');
$writer->startElement('head');$writer->startElement('title');$writer->writeText('Our teas');$writer->endElement();$writer->endElement();
$writer->startElement('body');$writer->startElement('ul');foreach ($items as string $item) { $writer->startElement('li'); $writer->writeText($item); $writer->endElement();}$writer->endElement();$writer->endElement();
$writer->endElement();echo $writer->endDocument(), "\n";<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Our teas</title> </head> <body> <ul> <li>Green tea</li> <li>Black tea</li> <li>Herbal tea</li> </ul> </body></html>