Dom\Attr::rename

(PHP 8 >= 8.4.0)

Dom\Attr::rename β€” Changes the qualified name or namespace of an attribute

Опис

public Dom\Attr::rename(?string $namespaceURI, string $qualifiedName): void

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

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΈ

namespaceURI
The new namespace URI of the attribute.
qualifiedName
The new qualified name of the attribute.

ЗначСння, Ρ‰ΠΎ ΠΏΠΎΠ²Π΅Ρ€Ρ‚Π°ΡŽΡ‚ΡŒΡΡ

НС ΠΏΠΎΠ²Π΅Ρ€Ρ‚Π°Ρ” Π·Π½Π°Ρ‡Π΅Π½ΡŒ.

Помилки/Π²ΠΈΠΊΠ»ΡŽΡ‡Π΅Π½Π½Ρ

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.

ΠŸΡ€ΠΈΠΊΠ»Π°Π΄ΠΈ

ΠŸΡ€ΠΈΠΊΠ»Π°Π΄ #1 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

$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();

?>

Поданий Π²ΠΈΡ‰Π΅ ΠΏΡ€ΠΈΠΊΠ»Π°Π΄ Π²ΠΈΠ²Π΅Π΄Π΅:

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

ΠŸΡ€ΠΈΠΊΠ»Π°Π΄ #2 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

$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();

?>

Поданий Π²ΠΈΡ‰Π΅ ΠΏΡ€ΠΈΠΊΠ»Π°Π΄ Π²ΠΈΠ²Π΅Π΄Π΅:

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

ΠŸΡ€ΠΈΠΌΡ–Ρ‚ΠΊΠΈ

ЗауваТСння: It is sometimes necessary to change the qualified name and namespace URI together in one step to not break any namespace rules.

ΠŸΡ€ΠΎΠ³Π»ΡΠ½ΡŒΡ‚Π΅ Ρ‚Π°ΠΊΠΎΠΆ

  • Dom\Element::rename()
οΌ‹add a note

User Contributed Notes

There are no user contributed notes for this page.