Core\Xml\Node::attributes
Returns the attributes of an XML element as an array. An attribute is a name and a value written inside a start tag, such as href in <link href="page.html"/>.
The array is keyed by the attribute name, and the order is the order in the document. The name keeps its prefix, such as xml:lang. Entities such as & are already expanded in the value. A tab or a line break written inside a value becomes a space, because XML requires this.
For a node that is not an element, the array is empty. This is not an error, so you can call the method on every child in a loop. Every value is tainted, because it came from the document.
The examples below list all attributes of an element, use a default value when an attribute is missing, and find the web page link in a news feed.
Return value
Returns array<tainted string>. An array keyed by attribute name. The name is written as in the document, with its prefix, such as xml:lang. Entities such as & are expanded in the value. A tab or a line break written inside the value becomes a space, as XML requires. The values are tainted.
Examples
List the attributes
<?nvs// Shows how to list every attribute of an element, in the order the document wrote them.
Core\Xml\Node $document = Core\Xml::parse('<image src="cat.png" width="640" height="480" alt="A cat & a dog"/>');
foreach ($document->children() as Core\Xml\Node $image) { // The array is keyed by the attribute name. `&` in the value is read as `&`. // This prints four lines, starting with src = cat.png. foreach ($image->attributes() as tainted string $name => tainted string $value) { echo $name, ' = ', $value, "\n"; }}src = cat.pngwidth = 640height = 480alt = A cat & a dogAn attribute that is missing
<?nvs// Shows how to read one attribute, and use a default value when the element does not have it.
Core\Xml\Node $document = Core\Xml::parse('<prices><price currency="USD">12.50</price><price>9.90</price></prices>');
foreach ($document->children() as Core\Xml\Node $prices) { foreach ($prices->children() as Core\Xml\Node $price) { array<tainted string> $attributes = $price->attributes(); // The second price has no `currency` attribute, so `??` gives "EUR". tainted string $currency = $attributes['currency'] ?? 'EUR'; // A text node is not an element, so its array is empty. foreach ($price->children() as Core\Xml\Node $text) { echo $text->text(), ' ', $currency, ' (the text has ', Core\Arr::count($text->attributes()), " attributes)\n"; } }}12.50 USD (the text has 0 attributes)9.90 EUR (the text has 0 attributes)Find the link in a news feed
<?nvs// Shows how to find the web page of a news feed. An Atom feed has several `link`// elements, and the one with `rel="alternate"` points to the web page.
string $feed = '<feed xmlns="http://www.w3.org/2005/Atom"> <title>Shop news</title> <link rel="self" href="https://example.com/news.xml"/> <link rel="alternate" type="text/html" href="https://example.com/news"/></feed>';
Core\Xml\Node $document = Core\Xml::parse($feed);
foreach ($document->children() as Core\Xml\Node $root) { foreach ($root->children() as Core\Xml\Node $child) { // The line breaks between the elements are text nodes. They have no attributes. array<tainted string> $attributes = $child->attributes(); if ($child->name() == 'link' && ($attributes['rel'] ?? '') == 'alternate') { // This prints https://example.com/news. echo 'web page: ', $attributes['href'], "\n"; } }}web page: https://example.com/news