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

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -