2024 日本 PHP 研討會

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 現在,呼叫此方法而不指定前綴將會選擇一個前綴,而不是假設為預設命名空間。先前,這會建立沒有前綴的屬性,並錯誤地將命名空間應用於擁有者元素,因為預設命名空間不適用於屬性。
8.3.0 現在,使用已在擁有者元素上以不同命名空間 URI 宣告的前綴呼叫此方法將會更改新前綴,以避免建立命名空間衝突。這使行為與 DOM 規範一致。先前,這會引發程式碼為 DOM_NAMESPACE_ERRDOMException

參見

新增註解

使用者貢獻的註解 1 則註解

_ michael
14 年前
如果在建立和插入屬性時引入了新的命名空間,createAttributeNS() 的行為與 createElementNS() 不同。

(1) 位置:使用 createAttributeNS() 時,新的命名空間會在文件元素的層級宣告。相反地,createElementNS() 會在受影響元素本身的層級宣告新的命名空間。

(2) 時機:使用 createAttributeNS() 時,新的命名空間會在屬性建立後立即在文件中宣告,實際上並不需要插入屬性。 createElementNS() 在元素未插入前不會影響文件。

範例

<?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