CakeFest 2024: The Official CakePHP Conference

DOMDocument::createAttributeNS

(PHP 5, PHP 7, PHP 8)

DOMDocument::createAttributeNS 関連付けられた名前空間に新しい属性を作成する

説明

public DOMDocument::createAttributeNS(?string $namespace, string $qualifiedName): DOMAttr|false

この関数は、DOMAttr クラスの新しいインスタンスを作成します。 このノードは、( DOMNode::appendChild() などで) 挿入されない限り、ドキュメント内にあらわれません。

パラメータ

namespace

名前空間の URI。

qualifiedName

属性のタグ名とプレフィックスを、prefix:tagname のような形式で指定する。

戻り値

新しい DOMAttr、あるいはエラーが発生した場合は false を返します。

エラー / 例外

DOM_INVALID_CHARACTER_ERR

qualifiedName が無効な文字を含んでいる場合に発生します。

DOM_NAMESPACE_ERR

qualifiedName が不正な形式である場合、あるいは qualifiedName がプレフィックスを含んでいる にもかかわらず namespacenull である場合に発生します。

変更履歴

バージョン 説明
8.3.0 prefix を指定せずにこのメソッドをコールしても、デフォルトの名前空間を仮定する代わりに prefix を選ぶようになりました。これより前のバージョンでは、prefix なしの属性を作成し、誤ってオーナーとなる要素の名前空間を適用していました。なぜなら、デフォルトの名前空間は属性には適用されないためです。
8.3.0 オーナーとなる要素で prefix が既に異なる namespace URI で使われている場合にこのメソッドを呼び出しても、名前空間の衝突を避けるために新しく prefix を異なるものに変更するようになりました。これは DOM の仕様が要求する振る舞いに合わせるためです。これより前のバージョンでは、エラーコード DOM_NAMESPACE_ERR を持つ DOMException をスローしていました。

参考

add a note

User Contributed Notes 1 note

up
9
_ michael
13 years ago
If a new namespace is introduced while creating and inserting an attribute, createAttributeNS() does not behave in the same way as createElementNS().

(1) Location: With createAttributeNS(), the new namespace is declared at the level of the document element. By contrast, createElementNS() declares the new namespace at the level of the affected element itself.

(2) Timing: With createAttributeNS(), the new namespace is declared in the document as soon as the attribute is created - the attribute does not actually have to be inserted. createElementNS() doesn't affect the document as long as the element is not inserted.

An example:

<?php

$source
= <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<root><tag></tag></root>
XML;

/*

I. createAttributeNS:
* a new namespace shows up immediately, even without insertion of the attribute
* the new namespace is declared at the level of the document element

*/

$doc = new DOMDocument( '1.0' );
$doc->loadXML( $source );

// (1) We just create a "namespace'd" attribute without appending it to any element.
$attr_ns = $doc->createAttributeNS( '{namespace_uri_here}', 'example:attr' );

print
$doc->saveXML() . "\n";

/*
Result: The namespace declaration appears, having been added to the document element. Output:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:example="{namespace_uri_here}"><tag/></root>

*/

// (2) Next, we give the attribute a value and insert it.
$attr_ns->value = 'value';
$doc->getElementsByTagName( 'tag' )->item(0)->appendChild( $attr_ns );

print
$doc->saveXML() . "\n";

/*
Result: The "namespace'd" attribute shows up as well. Output:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:example="{namespace_uri_here}"><tag example:attr="value"/></root>

*/

/*

II. createElementNS:
* a new namespace shows up only when the element is inserted
* the new namespace is declared at the level of the inserted element

*/

$doc = new DOMDocument( '1.0' );
$doc->loadXML( $source );

// (1) We create a "namespace'd" element without inserting it into the document.
$elem_ns = $doc->createElementNS( '{namespace_uri_here}', 'example:newtag' );

print
$doc->saveXML() . "\n";

/*
Result: The document remains unchanged. Output:

<?xml version="1.0" encoding="UTF-8"?>
<root><tag/></root>

*/

// (2) Next, we insert the new element.
$doc->getElementsByTagName( 'tag' )->item(0)->appendChild( $elem_ns );

print
$doc->saveXML() . "\n";

/*
Result: The namespace declaration appears, and it is embedded in the element using it. Output:

<?xml version="1.0" encoding="UTF-8"?>
<root><tag><example:newtag xmlns:example="{namespace_uri_here}"/></tag></root>

*/

?>
To Top