Upsert an XML attribute


In order to upsert an attribute in an XML file, you need to have both an insert and an update in the same command.

Example that adds a style attribute to the root >svg< element of every SVG file.

xmlstarlet edit \
	--inplace \
	--update '/_:svg/@style' -x 'string("background-color: white")' \
	--insert '/_:svg[not(@style)]' --type attr -n newattr --value 'background-color: white' \
	*.svg

Date: 2025-02-08

Tags:  xml

Share: