How to insert elements with attributes to an XML file using XmlStarlet? How to insert elements with attributes to an XML file using XmlStarlet? shell shell

How to insert elements with attributes to an XML file using XmlStarlet?


Answers

  1. /xml/block/el[not(@name)]
  2. As stated in an other answer:

You can't insert an element with an attribute directly but since every edit operation is performed in sequence, you can insert an element and then add an attribute.

The command

xmlstarlet ed -a '/xml/block/el[@name="b"]' \              -t 'elem' -n 'el' -v 0 \              -i '/xml/block/el[not(@name)]' \              -t 'attr' -n 'name' -v 'c'