Deprecated: Optional parameter $keys declared before required parameter $cms_id is implicitly treated as a required parameter in /home/www/dev/work/class/blog/CmsKey.php on line 75

Deprecated: Creation of dynamic property lvesu\lvesu\controller\blog\php::$title is deprecated in /home/www/dev/work/website/lvesu/class/controller/blog/php.php on line 28

Deprecated: Creation of dynamic property lvesu\lvesu\controller\blog\php::$outlink is deprecated in /home/www/dev/work/website/lvesu/template/blog/cms/php.manual.tpl on line 2

Deprecated: Creation of dynamic property lvesu\lvesu\controller\blog\php::$status is deprecated in /home/www/dev/work/website/lvesu/template/blog/index.head.php on line 2
PHP - Manual: Dom\Attr::rename - 互联网笔记

略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: Dom\Attr::rename

2025-06-02

Dom\Attr::rename

(PHP 8 >= 8.4.0)

Dom\Attr::renameChanges 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()
添加备注

用户贡献的备注

此页面尚无用户贡献的备注。

官方地址:https://www.php.net/manual/en/dom-attr.rename.php

冷却塔厂家 广告
中文GPT4.0无需注册 广告
北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3