Remove node in SVG by id using xmlstarlet
I have a folder with many SVGs in them. From all SVGs, I need to remove a tag with a specific id. There are too many files to do this manually, so I worked on a shell script to do this for me, but cannot figure out how to use xmlstarlet to remove the tag.
For example, let's say we have foo.svg:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg">
<g id="delete"/>
<g id="keep"/>
</svg>
Then the command is issued: xmlstarlet ed -d "g[@id='delete']" foo.svg > bar.svg
will result in the following:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" xmlns="http://www.w3.org/2000/svg">
<g id="keep"/>
</svg>
but this is not the case. bar.foo still contains <g id="delete"/>
.
Does anyone know the correct command to remove a tag <g id="delete"/>
? Or any other tool I could use to batch precess the SVG files to get rid of the unwanted tag?
Cheers, Dominic
source to share
SVG uses namespaces, I think you'll have to define them: documentation
Then the correct syntax is as follows: xmlstarlet ed -N ns=http://www.w3.org/2000/svg -d "//ns:g[contains(@id,'delete')]" foo.svg > bar.svg
source to share