1--TEST-- 2DOM cloneNode : Basic Functionality 3--SKIPIF-- 4<?php 5require_once('skipif.inc'); 6?> 7--CREDITS-- 8Simon Hughes <odbc3@hotmail.com> 9--FILE-- 10<?php 11 12$xml = <<< EOXML 13<?xml version="1.0" encoding="ISO-8859-1"?> 14<courses> 15 <course title="one"> 16 <notes> 17 <note>c1n1</note> 18 <note>c1n2</note> 19 </notes> 20 </course> 21 <course title="two"> 22 <notes> 23 <note>c2n1</note> 24 <note>c2n2</note> 25 </notes> 26 </course> 27</courses> 28EOXML; 29 30function dumpcourse($current) { 31 $title = ($current->nodeType != XML_TEXT_NODE && $current->hasAttribute('title')) ? $current->getAttribute('title'):"no title"; 32 echo "Course: $title:";echo(get_class($current)), "\n"; 33 echo "~";var_dump($current->textContent); 34} 35 36$dom = new DOMDocument(); 37$dom->loadXML($xml); 38$root = $dom->documentElement; 39 40// strip all text nodes from this tree 41$children = $root->childNodes; 42$len = $children->length; 43for ($index = $children->length - 1; $index >=0; $index--) { 44 $current = $children->item($index); 45 if ($current->nodeType == XML_TEXT_NODE) { 46 $noderemoved = $root->removeChild($current); 47 } 48} 49 50echo "Start cloneNode test\n"; 51$first_course = $children->item(0); 52$cloned_first_course_default = $first_course->cloneNode(); 53$first_course->setAttribute('title', 'new title1'); 54 55$cloned_first_course_true = $first_course->cloneNode(true); 56$first_course->setAttribute('title', 'new title2'); 57 58$cloned_first_course_false = $first_course->cloneNode(false); 59$first_course->setAttribute('title', 'new title3'); 60 61$cloned_first_course_default->setAttribute('title', 'new title default'); 62$cloned_first_course_true->setAttribute('title', 'new title true'); 63$cloned_first_course_false->setAttribute('title', 'new title false'); 64 65$root->appendChild($cloned_first_course_default); 66$root->appendChild($cloned_first_course_true); 67$root->appendChild($cloned_first_course_false); 68 69$children = $root->childNodes; 70for ($index = 0; $index < $children->length; $index++) { 71 echo "node $index\n"; 72 dumpcourse($children->item($index)); 73} 74?> 75--EXPECT-- 76Start cloneNode test 77node 0 78Course: new title3:DOMElement 79~string(57) " 80 81 c1n1 82 c1n2 83 84 " 85node 1 86Course: two:DOMElement 87~string(57) " 88 89 c2n1 90 c2n2 91 92 " 93node 2 94Course: new title default:DOMElement 95~string(0) "" 96node 3 97Course: new title true:DOMElement 98~string(57) " 99 100 c1n1 101 c1n2 102 103 " 104node 4 105Course: new title false:DOMElement 106~string(0) "" 107