Delete an XML node from an XML file on the command line

Let's pretend you had this XML fil. (It's an XML data from Open Street Map)

<?xml version="1.0" encoding="UTF-8"?> <osm version="0.6" generator="CGImap 0.0.2"> <bounds minlat="53.2875000" minlon="-6.2274000" maxlat="53.3046000" maxlon="-6.1941000"/> <node id="389719" lat="53.3067754" lon="-6.2129946" user="Jazzmax" uid="110583" visible="true" version="2" changeset="21774" timestamp="2009-04-01T03:08:52Z"/> <node id="19223905" lat="53.2954887" lon="-6.2035759" user="mackerski" uid="6367" visible="true" version="1" changeset="397382" timestamp="2007-09-08T23:45:14Z"> <tag k="created_by" v="JOSM"/> </node> <node id="19223906" lat="53.2941060" lon="-6.2025008" user="Nick Burrett" uid="166593" visible="true" version="5" changeset="6041772" timestamp="2010-10-14T18:29:52Z"> <tag k="highway" v="traffic_signals"/> </node> </osm>

You can use xmlstarlet (Ubuntu users: click here to install it) to remove all the tag nodes from this document.

This will delete any tag node that is in a node tag, which is in an osm tag. It takes the input form input.xml and writes the output to output.xml:

xmlstarlet ed --delete "/osm/node/tag" input.xml > output.xml

You can also use -d to remove the tags:

xmlstarlet ed -d "/osm/node/tag" input.xml > output.xml

Related: Adding an XML attribute to nodes on the command line

This entry is tagged: