0

I'm trying to unset a node from a web.config file but it doesn't seem to be working. Anyone know what I'm doing wrong? If there's a better approche please let me know?

            $web_config = simplexml_load_file('web.config');

            $nodes = $web_config->children();

            $att_name = 'myMap';
            $value = '1';

            $map_node = $nodes[0]->xpath( sprintf('rewrite/rewriteMaps/rewriteMap[@name="%s"]/add[@value="%d"]', $att_name, $value) );

            print_r($map_node); // this outpus the correct node

            if (!empty($map_node)) {
                unset($map_node)
            } else {
                printf('No maps with value: "%d" found', $value);
            }

            $web_config->asXML(); 
Pardoner
  • 1,011
  • 4
  • 16
  • 28
  • This seems to work `unset( $nodes[0]->rewrite->rewriteMaps );` but I'm stuck at the attribute value. How would I get the rewriteMaps node with the correct attribute (name=myMap)? – Pardoner Feb 03 '11 at 00:18
  • `$map_node` is an array of the matching `add` elements. You don't want to be unsetting an array! See the duplicate question for how to unset the SimpleXMLElement(s). – salathe Feb 03 '11 at 07:48

1 Answers1

-1
$web_config = new SimpleXMLElement('web.config',null,true);
$map_node = $web_config->xpath( sprintf('//rewrite/rewriteMaps/rewriteMap[@name="%s"/add[@value="%d"]', 'myMap', 1) );

if (!empty($map_node)) {
    unset($map_node[0][0]);
}

$web_config->asXml() 
hakre
  • 193,403
  • 52
  • 435
  • 836
jwerre
  • 9,179
  • 9
  • 60
  • 69