I think you are jumbling two classes' methods together.
SimpleXMLElement::removeChild
does not exist (it is part of DOMNode
). Unfortunately, DOMDocument::xpath
also does not exist, though, so you can't use either one without modifying your code.
I chose DOMDocument
because of the simplicity of your DOM selection (just needing elements by the tag name), and here are my results:
<?php
$bad = 'Value';
$doc = DOMDocument::load('./xpath_del.xml') or die('Failed parsing XML');
$x = $doc->getElementsByTagName('x');
for ($i = 0; $i < $x->length; ++$i) {
$z = $x->item($i)->getElementsByTagName('z');
for ($j = 0; $j < $z->length; ++$j) {
if ($bad === $z->item($i)->nodeValue) {
$x->item($i)->parentNode->removeChild($x->item($j));
break;
}
}
}
?>
So, using conversion to SimpleXMLElement
for debugging purposes (because I'm too lazy to write a method to traverse the DOMNode
s in the DOMDocument
):
var_dump(simplexml_import_dom($doc));
to output the DOMDocument
structure before and after. Remember, if this is for a high traffic site, I'd absolutely recommend not leaving this debugging code in (but this might not matter to you).
Anyway, here are the results before:
object(SimpleXMLElement)#2 (1) {
["x"]=>
object(SimpleXMLElement)#3 (1) {
["y"]=>
object(SimpleXMLElement)#4 (1) {
["z"]=>
string(5) "Value"
}
}
}
And after:
object(SimpleXMLElement)#5 (0) {
}
Try it out for yourself, ;)