I have the xml file "flashcards.xml":
<flashcards>
<category name="testCategory">
<card front="A" back="B"/>
<card front="C" back="D"/>
<card front="E" back="F"/>
</category>
</flashcards>
I try to remove a category by given attribute "name" like this in php:
<?php
$xml = simplexml_load_file("flashcards.xml");
if (isset($_POST['remCat'])) {
$remCat = $_POST['remCat'];
$path = 'category[@name="' . $remCat . "\"]";
$allCategories = $xml->xpath($path);
$toRemove = $allCategories[0];
unset($xml->$toRemove);
var_dump($xml->asxml());
}
?>
This code should load the file, looks for the category child with the attribute "remCat" and unsets it. If I print out $toRemove
its the right node, but var_dump($xml->asxml());
prints the unchanged file. The child is not removed.