xmlstarlet: How to conditionally add missing element (upsert) -
using xmlstarlet, want add property list,
<document> <properties> <property>...</property> <property>...</property> <!-- add this! --> </properties> </document>
which easy, except "properties" tag optional , may missing in original document, in case "properties" tag needs conditionally created.
the ed
subcommand doesn't have conditionals, there no nice way, think insert new properties
element , delete if turns out "extra" (i.e. not first):
xmlstarlet ed \ -s /document -t elem -n properties -v '' \ -d '/document/properties[position() != 1]' \ -s /document/properties -t elem -n property -v 'new property value' \ doc.xml
otherwise, check first sel
, use shell conditionals decide whether insertion needed.
Comments
Post a Comment