Insert a new XML element as the first child


Use xmlstarlet to insert a new element as the first child of a parent element:

xmlstarlet edit \
    --insert '/_:svg/*[1]' --type elem --name 'rect' --value '' \
    --insert '/_:svg/*[1]' --type attr --name 'x' --value '0' \
    --insert '/_:svg/*[1]' --type attr --name 'y' --value '0' \
    --insert '/_:svg/*[1]' --type attr --name 'width' --value '100' \
    --insert '/_:svg/*[1]' --type attr --name 'height' --value '100' \
    "$FILENAME"

Date: 2025-02-09

Tags:  xml

Share: