Core\Xml\Writer::writeInstruction
$writer->writeInstruction(string $target, string $data): void$writer->writeInstruction( string $target, string $data, ): void
Writes a processing instruction, such as <?xml-stylesheet href="feed.css" type="text/css"?>.
A processing instruction is a note for the program that reads the document. It has a target, which names the program or the purpose, and data, which is plain text. The writer writes the data unchanged, and it does not escape anything. When the data is "", only the target is written. You can write an instruction before the root element, inside an element or after it.
The target must be a valid XML name, and it cannot be xml in any casing. startDocument already writes that one. The data cannot contain ?>, because that ends the instruction. In each of these cases, writeInstruction throws a LogicError and writes nothing. A tainted string (text from a visitor) does not compile as the target or the data. This is because the program that reads the document acts on the data.
The examples below read an instruction back with the parser, show the three errors, and link a style sheet to a feed.
Parameters
$targetstringThe name of the program or purpose the instruction is for. It must be an XML name. It cannot bexmlin any casing, becausestartDocumentalready writes that one. Ataintedstring does not compile here.$datastringThe instruction's data. It is written unchanged, because XML cannot escape anything inside an instruction. It cannot contain?>. When it is"", only the target is written. Ataintedstring does not compile here, because the program that reads the document follows the data.
Return value
Returns void. Nothing. The instruction is written at the place the writer has reached.
Errors
LogicErrorThe target is not an XML name, or it isxml. The data contains?>. The data contains a control character that XML cannot write. 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
Write and read back
<?nvs// Shows how to write processing instructions, and how a parser reads their target and data.
Core\Xml\Writer $writer = Core\Xml::writer();$writer->startDocument();
// This instruction is before the root element. Its target is `app` and its data is `version="2"`.$writer->writeInstruction('app', 'version="2"');$writer->startElement('settings');// This instruction is inside the element. Its data is `""`, so only the target is written.$writer->writeInstruction('reload', '');$writer->endElement();
string $document = $writer->endDocument();echo $document, "\n";
// `name` returns the target and `text` returns the data.// This prints `app: version="2"` and then `reload: `.Core\Xml\Node $top = Core\Xml::parse($document);Core\Xml\Node $first = $top->children()[0];Core\Xml\Node $second = $top->children()[1]->children()[0];echo $first->name(), ': ', $first->text(), "\n";echo $second->name(), ': ', $second->text(), "\n";<?xml version="1.0" encoding="UTF-8"?><?app version="2"?><settings><?reload?></settings>app: version="2"reload:Targets and data that are not allowed
<?nvs// Shows the three errors `writeInstruction` throws, and that a failed call writes nothing.
Core\Xml\Writer $writer = Core\Xml::writer();$writer->startDocument();
// `startDocument` already wrote the `xml` declaration, so `xml` is not allowed as a target.// This prints the error message.try { $writer->writeInstruction('XML', "version=\"1.0\"");} catch (LogicError $error) { echo $error->message, "\n";}
// A target must be an XML name, so it cannot contain a space. This prints the error message.try { $writer->writeInstruction('my app', 'go');} catch (LogicError $error) { echo $error->message, "\n";}
// `?>` ends an instruction, so the data cannot contain it. This prints the error message.try { $writer->writeInstruction('app', 'done?> <admin/>');} catch (LogicError $error) { echo $error->message, "\n";}
// The three calls above wrote nothing. The document has only the root element.$writer->startElement('page');$writer->endElement();echo $writer->endDocument(), "\n";Core\Xml\Writer::writeInstruction(): `xml` is the declaration's own target, which `startDocument` writes.Core\Xml\Writer::writeInstruction(): `my app` is not a name a document can write.Core\Xml\Writer::writeInstruction(): a processing instruction has no escape grammar, so its data cannot hold `?>`.<?xml version="1.0" encoding="UTF-8"?><page/>A style sheet for a feed
<?nvs// Shows how to link a style sheet to a news feed, so a browser shows the feed as a formatted page.
array<string> $posts = [ 'New teas in stock' => 'https://example.com/news/new-teas', 'Summer sale' => 'https://example.com/news/summer-sale',];
Core\Xml\Writer $writer = Core\Xml::writer({ indent: ' ' });$writer->startDocument();// A browser reads this instruction and uses `feed.xsl` to show the feed.$writer->writeInstruction('xml-stylesheet', 'href="/feed.xsl" type="text/xsl"');$writer->startElement('rss');$writer->writeAttribute('version', '2.0');$writer->startElement('channel');
foreach ($posts as string $title => string $link) { $writer->startElement('item'); $writer->startElement('title'); $writer->writeText($title); $writer->endElement(); $writer->startElement('link'); $writer->writeText($link); $writer->endElement(); $writer->endElement();}
$writer->endElement();$writer->endElement();echo $writer->endDocument(), "\n";<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet href="/feed.xsl" type="text/xsl"?><rss version="2.0"> <channel> <item> <title>New teas in stock</title> <link>https://example.com/news/new-teas</link> </item> <item> <title>Summer sale</title> <link>https://example.com/news/summer-sale</link> </item> </channel></rss>