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