1--TEST--
2Replacing a child node
3--EXTENSIONS--
4dom
5--CREDITS--
6Matt Raines <matt@raines.me.uk>
7#London TestFest 2008
8--FILE--
9<?php
10$document = new DOMDocument();
11$document->loadXML('<?xml version="1.0" encoding="utf-8"?>
12<root><foo><bar/><baz/></foo><spam><eggs/><eggs/></spam></root>');
13
14// Replaces the child node oldChild with newChild in the list of children, and
15// returns the oldChild node.
16$parent = $document->getElementsByTagName('foo')->item(0);
17$new_child = $document->createElement('qux');
18$old_child = $parent->replaceChild($new_child, $parent->firstChild);
19echo "New child replaces old child:\n" . $document->saveXML();
20echo "Old child is returned:\n" . $old_child->tagName . "\n";
21
22// If the newChild is already in the tree, it is first removed.
23$parent = $document->getElementsByTagName('spam')->item(0);
24$parent->replaceChild($new_child, $parent->firstChild);
25echo "Existing child is removed from tree:\n" . $document->saveXML();
26
27// Children are inserted in the correct order.
28$new_child = $document->getElementsByTagName('spam')->item(0);
29$parent = $document->getElementsByTagName('foo')->item(0);
30$parent->replaceChild($new_child, $parent->firstChild);
31echo "Children are inserted in order:\n" . $document->saveXML();
32?>
33--EXPECT--
34New child replaces old child:
35<?xml version="1.0" encoding="utf-8"?>
36<root><foo><qux/><baz/></foo><spam><eggs/><eggs/></spam></root>
37Old child is returned:
38bar
39Existing child is removed from tree:
40<?xml version="1.0" encoding="utf-8"?>
41<root><foo><baz/></foo><spam><qux/><eggs/></spam></root>
42Children are inserted in order:
43<?xml version="1.0" encoding="utf-8"?>
44<root><foo><spam><qux/><eggs/></spam></foo></root>
45