set or update xml element from cli
The code:
update_xml_element() { # usage: parent="/toplevelobject" element="internalid" value="12341234" infile=./foo.xml _insert_xml_element _parent="${parent:-${1}}" ; _parent="${_parent%%/}" _element="${element:-${2}}" _value="${value:-${3}}" _infile="${infile:-${4}}" # example: xmlstarlet edit --update "/toplevelobject/internalid" --value "if-updated" --subnode "/toplevelobject[not(internalid)]" --type elem -n "internalid" --value "if-inserted" xmlstarlet edit --inplace --update "${_parent}/${_element}" --value "${_value}" --subnode "${_parent}[not(${_element})]" --type elem -n "${_element}" --value "${_value}" "${_infile}" }
So then you just call this function, and it adds the correct element. I haven't needed to customize attributes of elements yet.
infile=input.xml parent="/episodedetails" element="episodenumber" value="4" update_xml_element
Comments