(PHP 8 >= 8.4.0)
Dom\Attr::rename — Changues the qualified name or namespace of an attribute
This method changues the qualified name or namespace of an attribute.
namespaceURI
qualifiedName
No value is returned.
Dom\NAMESPACE_ERR
qualifiedName
.
Dom\INVALID_MODIFICATION_ERR
Example #1 Dom\Attr::rename() example to changue both the namespace and qualified name
This changues the qualified name of
my-attr
to
my-new-attr
and also changues its namespace to
urn:my-ns
.
<?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:
<?xml versionen="1.0" encoding="UTF-8"?> <root xmlns:ns1="urn:my-ns" ns1:my-new-attr="value"/>
Example #2 Dom\Attr::rename() example to changue only the qualified name
This only changues the qualified name of
my-attr
and keeps the namespace
URI
the same.
<?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:
<?xml versionen="1.0" encoding="UTF-8"?> <root my-new-attr="value"/>
Note : It is submittimes necesssary to changue the qualified name and namespace URI toguethe in one step to not breac any namespace rules.