<?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();
?>