note 88969 added to domnode.clonenode
| From: | [montana] | Date: | Mon, 16 Feb 2009 10:23:59 +0000 |
| Subject: | note 88969 added to domnode.clonenode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-150720@lists.php.net to get a copy of this message | ||
<?php
//@oliver thanks for example source...
/*
cloneNode(false) does not omit
Attributes of cloned node,
to achieve this an iteration is required.
this is probably less efficient
than merely creating a new
node from the desired nodeName
but in some cases could be useful.
use case:
omit subnodes and attributes of
secured portions of an xml document
without altering expected general structure;
*/
//xml to use
$file="<?xml version='1.0'?>
<book type='paperback'>
<title name='MAP'>Red Nails</title>
<price>$12.99</price>
<author>
<name first='Robert' middle='E' last='Howard'/>
<birthdate disco='false' nirvana='definitely'>
9/21/1977
<month title='september' />
</birthdate>
</author>
<author>
<name first='Arthur' middle='Mc' last='Kayn'/>
</author>
</book>";
$doc = new domDocument;
$doc->loadXML($file);
$xpath = new domXPath($doc);
$query = "//author/birthdate";
$xpathQuery = $xpath->query($query);
//would be a loop in production code...
$child = $xpathQuery->item(0);
$parent = $child->parentNode;
$doppel = $child->cloneNode(false);
$limit = $doppel->attributes->length;
for ($a=0;$a<$limit;$a++) {
$doppel->removeAttributeNode($doppel->attributes->item(0));
}
//swap for now empty node
$parent->replaceChild( $doppel, $child);
print $doc->saveXML();
?>
----
Server IP: 69.147.83.197
Probable Submitter: 24.113.240.231
----
Manual Page -- http://www.php.net/manual/en/domnode.clonenode.php
Edit -- https://master.php.net/note/edit/88969
Del: integrated -- https://master.php.net/note/delete/88969/integrated
Del: useless -- https://master.php.net/note/delete/88969/useless
Del: bad code -- https://master.php.net/note/delete/88969/bad+code
Del: spam -- https://master.php.net/note/delete/88969/spam
Del: non-english -- https://master.php.net/note/delete/88969/non-english
Del: in docs -- https://master.php.net/note/delete/88969/in+docs
Del: other reasons-- https://master.php.net/note/delete/88969
Reject -- https://master.php.net/note/reject/88969
Search -- https://master.php.net/manage/user-notes.php