Adding an attribute to an XML node in an XML file on the command line

Let's pretend you had this XML fil. (It's an XML file that's similar to 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"/> <tag></tag> </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"/> <tag></tag> </node> </osm>

You can use xmlstarlet (Ubuntu users: click here to install it) to add attributes to the empty tag nodes.

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 --insert "/osm/node/tag[not(@k)]" --type attr -n k -v foobar input.xml > output.xml

It will produce an XML file like this:

<?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"/> <tag k="foobar" /> </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"/> <tag k="foobar"/> </node> </osm>

Related: Deleting an XML node on the command line

This entry is tagged: