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--EXPECT-- 75Start cloneNode test 76node 0 77Course: new title3:DOMElement 78~string(24) " 79 80 c1n1 81 c1n2 82 83 " 84node 1 85Course: two:DOMElement 86~string(24) " 87 88 c2n1 89 c2n2 90 91 " 92node 2 93Course: new title default:DOMElement 94~string(0) "" 95node 3 96Course: new title true:DOMElement 97~string(24) " 98 99 c1n1 100 c1n2 101 102 " 103node 4 104Course: new title false:DOMElement 105~string(0) "" 106