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>
*/
?>
DOMDocument::createAttributeNS
(PHP 5)
DOMDocument::createAttributeNS — Yeni bir isim alanlı öznitelik düğümü oluşturur
Açıklama
Yeni bir DOMAttr nesnesi oluşturur. Bu düğüm, DomNode::append_child() gibi bir yöntemle belgeye yerleştirilmedikçe belgede gösterilmez.
Değiştirgeler
-
uri -
İsim alanını betimleyen adres.
-
önekliAd -
önek:öznitelik biçeminde etiket ismi.
Dönen Değerler
Bir hata oluşursa FALSE yoksa yeni
bir DOMAttr nesnesi döner.
Hatalar/İstisnalar
-
DOM_INVALID_CHARACTER_ERR -
önekliAdgeçersiz karakter içeriyorsa oluşur. -
DOM_NAMESPACE_ERR -
önekliAd, isim alanlı bir etiket adı olarak uygun değilse oluşur.
Ayrıca Bakınız
- DOMNode::appendChild() - Listenin sonuna yeni bir çocuk ekler
- DOMDocument::createAttribute() - Yeni bir öznitelik düğümü oluşturur
- DOMDocument::createCDATASection() - Yeni bir CDATA düğümü oluşturur
- DOMDocument::createComment() - Yeni bir açıklama düğümü oluşturur
- DOMDocument::createDocumentFragment() - Yeni bir belge bölütü oluşturur
- DOMDocument::createElement() - Yeni bir eleman düğümü oluşturur
- DOMDocument::createElementNS() - İsim alanlı bir eleman düğümü oluşturur
- DOMDocument::createEntityReference() - Yeni bir öğe bildirimi düğümü oluşturur
- DOMDocument::createProcessingInstruction() - Yeni bir işlem komutu düğümü oluşturur
- DOMDocument::createTextNode() - Yeni bir metin düğümü oluşturur
_ michael
01-Jun-2010 01:35
