W3docs

attributes()

Learn how to read, iterate, modify, add, and remove XML attributes with PHP SimpleXML — including the attributes() method and namespaced attributes.

Introduction

In XML, an attribute is a name/value pair attached to an element's opening tag, such as the isbn in <book isbn="123456789">. PHP SimpleXML exposes those attributes through a small, array-like API, so you can read, change, add, and remove them without parsing the XML by hand.

This page covers four everyday tasks — reading a single attribute, iterating over all of them with attributes(), modifying values, and adding new attributes — plus two things that trip people up: type casting and namespaced attributes.

Reading a single attribute

Treat the element like an array and index it by the attribute name:

php— editable, runs on the server

There is one common gotcha: $xml['isbn'] is not a plain string — it is a SimpleXMLElement. It prints fine because it is converted on the fly, but if you compare it strictly (===) or store it for later, cast it first:

<?php

$xml = new SimpleXMLElement('<book isbn="123456789"></book>');
$isbn = (string) $xml['isbn'];   // cast to a real string
var_dump($isbn === '123456789');
// bool(true)

Use isset() to check whether an attribute exists before reading it:

<?php

$xml = new SimpleXMLElement('<book isbn="123456789"></book>');
echo isset($xml['isbn']) ? "has isbn\n" : "no isbn\n";
echo isset($xml['lang']) ? "has lang\n" : "no lang\n";
// has isbn
// no lang

Listing every attribute with attributes()

When you don't know the attribute names in advance, call the attributes() method. It returns a SimpleXMLElement you can loop over, with the attribute name as the key and its value as the value:

<?php

$xml = new SimpleXMLElement('<book isbn="123456789" lang="en"><title>PHP Basics</title></book>');

foreach ($xml->attributes() as $name => $value) {
    echo "$name = $value\n";
}
// isbn = 123456789
// lang = en

Because the result is countable, you can also get the number of attributes:

<?php

$xml = new SimpleXMLElement('<book isbn="123456789" lang="en"></book>');
echo count($xml->attributes());
// 2

Modifying attribute values

To change an attribute, assign a new value to it through the array syntax, then serialize with asXML():

php— editable, runs on the server

Adding attributes

A new element from simplexml_load_string() or new SimpleXMLElement() may not have the attribute you need. Add one with addAttribute(), passing the name and value:

php— editable, runs on the server

Note that assigning to an attribute that does not exist yet ($xml['isbn'] = '...') also creates it — but addAttribute() is the only way to add a namespaced attribute.

Removing an attribute

There is no dedicated "remove" method. Unset the attribute with unset():

<?php

$xml = new SimpleXMLElement('<book isbn="123456789" lang="en"></book>');
unset($xml['lang']);
echo $xml->asXML();
// <?xml version="1.0"?>
// <book isbn="123456789"/>

Namespaced attributes

Attributes that belong to an XML namespace (for example meta:rating) are hidden from a plain attributes() call. Pass the namespace URI to read them:

<?php

$xml = new SimpleXMLElement(
    '<book xmlns:meta="http://example.com/meta" meta:rating="5"><title>PHP Basics</title></book>'
);

foreach ($xml->attributes('http://example.com/meta') as $name => $value) {
    echo "$name = $value\n";
}
// rating = 5

Summary

  • Read a single attribute with array syntax: $xml['isbn']. Cast it to (string) for strict comparisons.
  • Iterate all attributes with attributes(); it is loopable and countable.
  • Assign a new value to modify, addAttribute() to add, and unset() to remove.
  • Pass a namespace URI to attributes() to reach namespaced attributes.

To go further, see SimpleXML overview, simplexml_load_string(), and parsing XML with SimpleXML.

Practice

Practice
How do you read every attribute of a SimpleXML element when you don't know their names?
How do you read every attribute of a SimpleXML element when you don't know their names?
Was this page helpful?