DOMNamedNodeMap クナ゚

(PHP 5, PHP 7, PHP 8)

クナ゚æĻ‚čρ

class DOMNamedNodeMap implements IteratorAggregate, Countable {
/* ãƒ—ãƒ­ãƒ‘ãƒ†ã‚Ŗ */
public readonly int $length;
/* ãƒĄã‚Ŋッド */
public function count(): int
public function getIterator(): Iterator
public function getNamedItem(string $qualifiedName): ?DOMNode
public function getNamedItemNS(?string $namespace, string $localName): ?DOMNode
public function item(int $index): ?DOMNode
}

ãƒ—ãƒ­ãƒ‘ãƒ†ã‚Ŗ

length

マップ内ぎノãƒŧドぎ数。子ノãƒŧãƒ‰ãŽã‚¤ãƒŗãƒ‡ãƒƒã‚¯ã‚šã¨ã—ãĻ有劚ãĒį¯„å›˛ã¯ 0 から length - 1 ぞで (严įĢ¯ã‚’åĢむ) です。

変更åąĨæ­´

バãƒŧã‚¸ãƒ§ãƒŗ čĒŦ明
8.5.0 DOMNamedNodeMap ã‚Ēブジェクトをクロãƒŧãƒŗã™ã‚‹ã¨å¤ąæ•—ã™ã‚‹ã‚ˆã†ãĢãĒりぞした。
8.0.0 åŽŸčŖ…ã•ã‚ŒãĻいãĒã‹ãŖãŸãƒĄã‚Ŋッド DOMNamedNodeMap::setNamedItem(), DOMNamedNodeMap::removeNamedItem(), DOMNamedNodeMap::setNamedItemNS(), DOMNamedNodeMap::removeNamedItem() が削除されぞした。
8.0.0 DOMNamedNodeMap は IteratorAggregate ã‚’åŽŸčŖ…ã—ãžã—ãŸã€‚ これより前ぎバãƒŧã‚¸ãƒ§ãƒŗã§ã¯ã€ Traversable がäģŖã‚ã‚ŠãĢåŽŸčŖ…ã•ã‚ŒãĻいぞした。

æŗ¨æ„

æŗ¨æ„:

マップ内ぎノãƒŧãƒ‰ã¯ã€é…åˆ—ãŽæ–‡æŗ•ã‚’äŊŋãŖãĻã‚ĸクã‚ģ゚できぞす。

į›ŽæŦĄ

  • DOMNamedNodeMap::count — マップãĢあるノãƒŧドぎ数を取垗する
  • DOMNamedNodeMap::getIterator — 外部ぎイテãƒŦãƒŧã‚ŋを取垗する
  • DOMNamedNodeMap::getNamedItem — 名前で指厚されたノãƒŧドを取垗する
  • DOMNamedNodeMap::getNamedItemNS — ロãƒŧã‚ĢãƒĢåãŠã‚ˆãŗåå‰įŠē間 URI で指厚したノãƒŧドを取垗する
  • DOMNamedNodeMap::item — ã‚¤ãƒŗãƒ‡ãƒƒã‚¯ã‚šã§æŒ‡åŽšã—ãŸãƒŽãƒŧドを取垗する
īŧ‹add a note

User Contributed Notes 3 notes

up
7
kendsnyder at gmail dot com Âļ
16 years ago
To add to xafford's comment. When iterating a named node map collection using ->item() or using foreach, removing a attribute with DOMNode->removeAttribute() or DOMNode->removeAttributeNode() alters the collection as if it were a stack. To illustrate, the code below tries to remove all attributes from each element but only removes the first. One work around is to copy the named node map into an array before removing attributes. Using PHP 5.2.9 on Windows XP.

<?php
error_reporting(E_ALL);
$html  = '<h1 id="h1test" class="h1test">Heading</h1>';
$html .= '<p align="left" class="ptest">Hello World</p>';

$doc = new DOMDocument();
$doc->loadHTML($html);

// remove attributes from  the h1 element
$h1 = $doc->getElementsByTagName('h1')->item(0);
$length = $h1->attributes->length;
for ($i = 0; $i < $length; ++$i) {
    $name = $h1->attributes->item($i)->name;
    $h1->removeAttribute($name);
    echo "h1: removed attribute `$name`<br>";
}
// remove attributes from the p element
$p = $doc->getElementsByTagName('p')->item(0);
foreach ($p->attributes as $name => $attrNode) {
    $p->removeAttribute($name);
    echo "p: removed attribute `$name`<br>";
}

?>

OUTPUT:
-------
h1: removed attribute `id`

Notice: Trying to get property of non-object in nodemap.php on line 13
h1: removed attribute ``
p: removed attribute `align`
up
7
xafford Âļ
16 years ago
I stumbled upon a problem with DOMNamedNodeMap. If you iterate over a DOMNamedNodeMap, representing the attributes of a DOMElement with foreach and you use DOMElement::removeAttributeNode only the first attribute will be handled.

Example (not complete):

<?php

/*
 * Imagine you got a node like this:
 * <a onclick="alert('evil')" href="http://example.com">evil</a>
 * and onclick should be removed, href would not be tested.
 */

foreach ( $node->attributes as $attribute )
{

    echo 'checking attribute ', $attribute->name, '<br />';

    if ( ! in_array ( $attribute->name, $allowed_attributes ) )
    {
        $node->removeAttributeNode ( $attribute );
    }

}

?>

The output would be:

checking attribute onclick
up
4
sirajshaikh96 at yahoo dot com Âļ
5 years ago
I have tried to solve the general problem occurs in DOMNamedNodeMap that during executing for/foreachloop after executing removeAttribute('$name') for item(0), item(1) is not executing and warning occurs. Following codes give the solution of this problem that in loop item(0) should be applied rather than item($i) because after removing first attribute node now present element has only one attribute node.

<?php
$html  = '<h1 id="h1test" class="h2test">Heading</h1>';
$html .= '<p align="left" class="right">Hello World</p>';

$doc = new DOMDocument();
$doc->loadHTML($html);

// remove attributes from  the h1 element
$h1 = $doc->getElementsByTagName('h1')->item(0);
$length = $h1->attributes->length;
for ($i = 0; $i < $length; $i++) {    
    $name = $h1->attributes->item(0)->nodeName;
    $value = $h1->attributes->item(0)->nodeValue;
    $h1->removeAttribute($name);    
    echo "h1: removed attribute name :- " .$name."</br>";
    echo "h1: removed attribute value :- " .$value."</br>";   
    } 
// remove attributes from the p element
$p = $doc->getElementsByTagName('p')->item(0);
for ($i = 0; $i < $length; $i++) {        
    $name = $p->attributes->item(0)->nodeName;
    $value = $p->attributes->item(0)->nodeValue;
    $p->removeAttribute($name);    
    echo "p: removed attribute name :- " .$name."</br>";
    echo "p: removed attribute value :- " .$value."</br>";   
}
?>

OUTPUT:

h1: removed attribute name :- id
h1: removed attribute value :- h1test
h1: removed attribute name :- class
h1: removed attribute value :- h2test
p: removed attribute name :- align
p: removed attribute value :- left
p: removed attribute name :- class
p: removed attribute value :- right