Core\Xml\Writer::writeAttribute
$writer->writeAttribute(string $name, string $value): void$writer->writeAttribute( string $name, string $value, ): void
Adds one attribute, a name and a value, to the element that startElement opened last.
Call writeAttribute right after startElement, before you write any content or child element. After that, the start tag is closed, and writeAttribute throws a LogicError. An element can have each attribute name only once, so a second call with the same name also throws a LogicError.
The writer escapes the value. Quotes, <, & and line breaks are written as references, so a parser that reads the document gets back exactly the text you passed. This is also why a tainted value is allowed.
The examples below add two attributes and show how the writer escapes them, show the two errors, and export a product list with the product data in attributes.
Parameters
$namestringThe attribute's name, such asidorxml:lang. It must be a valid XML name.$valuestringThe attribute's value. When a parser reads the document, it gets back exactly this text. Ataintedvalue is allowed, because the writer escapes it.
Return value
Returns void. Nothing. The attribute is written in the element's start tag.
Errors
LogicErrorSomething was already written inside the element, or no element is open. The element already has an attribute with this name. The name is not a valid XML name. The value 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
Add attributes to an element
<?nvs// Shows how to add attributes to an element, and how the writer escapes their values.
Core\Xml\Writer $writer = Core\Xml::writer();$writer->startDocument();$writer->startElement('link');
// Each call adds one attribute to the element that is open.// The writer escapes the values. `&` is written as `&`, and `"` is written as `"`.$writer->writeAttribute('href', '/search?q=tea&page=2');$writer->writeAttribute('title', "Search for \"tea\"");
$writer->endElement();echo $writer->endDocument(), "\n";<?xml version="1.0" encoding="UTF-8"?><link href="/search?q=tea&page=2" title="Search for "tea""/>Attributes come first
<?nvs// Shows the two mistakes `writeAttribute` does not allow, and the error each one throws.
Core\Xml\Writer $writer = Core\Xml::writer();$writer->startDocument();$writer->startElement('note');$writer->writeAttribute('lang', 'en');
// An element can have each attribute name only once.try { $writer->writeAttribute('lang', 'de');} catch (LogicError $error) { echo $error->message, "\n";}
// After you write text inside an element, you cannot add an attribute to it.$writer->writeText('Back at noon.');try { $writer->writeAttribute('author', 'Sam');} catch (LogicError $error) { echo $error->message, "\n";}
// The calls that threw an error wrote nothing.$writer->endElement();echo $writer->endDocument(), "\n";Core\Xml\Writer::writeAttribute(): this element already carries an attribute named `lang`.Core\Xml\Writer::writeAttribute(): no start tag is still taking attributes — an attribute goes on its element before anything is written inside it.<?xml version="1.0" encoding="UTF-8"?><note lang="en">Back at noon.</note>Export products as xml
<?nvs// Shows how to export a list of products as an XML feed, with the product data in attributes.
array<string> $prices = ['TEA-01' => '4.50', 'MUG-07' => '12.00', 'POT-02' => '29.90'];
// The `indent` option puts each element on its own line.Core\Xml\Writer $writer = Core\Xml::writer({ indent: ' ' });$writer->startDocument();$writer->startElement('products');$writer->writeAttribute('currency', 'EUR');
// Each product has no content, so the writer closes its tag with `/>`.foreach ($prices as string $sku => string $price) { $writer->startElement('product'); $writer->writeAttribute('sku', $sku); $writer->writeAttribute('price', $price); $writer->endElement();}
$writer->endElement();echo $writer->endDocument(), "\n";<?xml version="1.0" encoding="UTF-8"?><products currency="EUR"> <product sku="TEA-01" price="4.50"/> <product sku="MUG-07" price="12.00"/> <product sku="POT-02" price="29.90"/></products>