Question: I have a XML file, MyFile.xml on my Linux machine.

```
<?xml version="1.0" encoding="UTF-8"?>
  <project>
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
          <modelVersion>4.0.0</modelVersion>
          <groupId>com.mycompany</groupId>
              <env name="envvar" value="/opt/environment/environment.properties" />
              <env name="JDBC_Driver"  value="/opt/JDBC/ojdb6.jar" />
              <env name="agent/enable" value="true" />
</project>

```

I need to change the value to `"false"` (when it is true) and `"true"` (when it is false) for the `env name="agent/enable"` tag in the XML file whenever I get a requirement.

I just wanted to use some shell script for this. I know this can be done using Sed command in Linux, but I'm not good at regular expressions. So that whenever I get a requirement to change I can just run a script.

Note: script should change the XML file and should be saved on the file system with same name.

Answer: 
Do not attempt to edit XML with plain-text tools; it can only end badly. Nobody expects their XML tools to break when they insert a newline into the XML or change a `<foo/>` node to `<foo></foo>`, but they will if they're based on sed (and not ridiculously complex).

Instead, use a proper XML-parsing tool such as `xmlstarlet`:

```
xmlstarlet ed --inplace -u '/project/env[@name="agent/enable"]/@value' -x 'string(not(. = "true"))' filename.xml

```

### Explanation

```
xmlstarlet ed --inplace -u <foo> -x <bar> filename.xml

```

updates nodes that fit the XPath expression `<foo>` to have the value of the XPath expression `<bar>`,

```
/project/env[@name="agent/enable"]/@value

```

is an XPath expression that selects the `value` attribute of `env` nodes whose `name` attribute is `agent/enable` and that are a child of the `project` root node -- this should describe the value you want to change adequately -- and

```
string(not(. = "true"))

```

is an XPath expression that is `false` if the currently inspected value is `"true"` and `true` otherwise. The currently inspected value is the old value of the `value` attribute that's being updated.
Here you are:

```
sed -i '/<env\s\+name="agent\/enable"/s/value="true"/value="false"/' f.xml

```