php8.5
Home/ Manual/ dom / attr/ Dom\Attr::rename

Dom\Attr::rename

PHP function Edit on GitHub ✎

(PHP 8 >= 8.4.0)

Changes the qualified name or namespace of an attribute

Description

Dom\Attr::rename(string|null $namespaceURI, string $qualifiedName): void

This method changes the qualified name or namespace of an attribute.

Parameters

namespaceURI

The new namespace URI of the attribute.

qualifiedName

The new qualified name of the attribute.

Return Values

Void

Errors/Exceptions

DOMException with code Dom\NAMESPACE_ERR

Raised if there is an error with the namespace, as determined by qualifiedName.

DOMException with code Dom\INVALID_MODIFICATION_ERR

Raised if there already exists an attribute in the element with the same qualified name.

Examples

Dom\Attr::rename example to change both the namespace and qualified name

This changes the qualified name of my-attr to my-new-attr and also changes its namespace to urn:my-ns.

php
<?php

$doc = Dom\XMLDocument::createFromString('<root my-attr="value"/>');

$root = $doc->documentElement;
$attribute = $root->attributes['my-attr'];
$attribute->rename('urn:my-ns', 'my-new-attr');

echo $doc->saveXml();

?>

The above example will output:

output
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:ns1="urn:my-ns" ns1:my-new-attr="value"/>

Dom\Attr::rename example to change only the qualified name

This only changes the qualified name of my-attr and keeps the namespace URI the same.

php
<?php

$doc = Dom\XMLDocument::createFromString('<root my-attr="value"/>');

$root = $doc->documentElement;
$attribute = $root->attributes['my-attr'];
$attribute->rename($attribute->namespaceURI, 'my-new-attr');

echo $doc->saveXml();

?>

The above example will output:

output
<?xml version="1.0" encoding="UTF-8"?>
<root my-new-attr="value"/>

Notes

Note

It is sometimes necessary to change the qualified name and namespace URI together in one step to not break any namespace rules.

See Also

  • Dom\Element::rename

Source: reference/dom/dom/attr/rename.xml · from the official PHP manual (php/doc-en)