<xml>
<block>
<el name="a">92346</el>
<el name="b">lorem</el>
</block>
<block>
<el name="a">89753</el>
<el name="b">ipsum</el>
</block>
</xml>
I would like to insert an <el name="c">0</el>
element in every <block>
with a Linux shell script:
<xml>
<block>
<el name="a">92346</el>
<el name="b">lorem</el>
<el name="c">0</el>
</block>
<block>
<el name="a">89753</el>
<el name="b">ipsum</el>
<el name="c">0</el>
</block>
</xml>
I can append the elements using XmlStarlet:
xmlstarlet ed -a '/xml/block/el[@name="b"]' \
--type 'elem' -n 'el' -v 0
<el>
element which
doesn't have a name
attribute?xml ed
command?/xml/block/el[not(@name)]
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.
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With