When you follow the doc:
xmlstarlet edit --help
you can read that --var
can be used to declare a XPath expression as a variable.
Generating moc file:
cat<<EOF > /tmp/file.xml
<root>
<elt>x</elt>
<!-- comment -->
<elt>y</elt>
<!-- other comment -->
</root>
EOF
This one works, without variables:
xmlstarlet edit \
--var xp '//elt/following::comment()' \
-a '//elt/following::comment()' -t elem -n p -v 'some new text' \
-a '//elt/following::comment()' -t elem -n p -v 'some other text' \
/tmp/file.xml
This one doesn't edit with variables:
xmlstarlet edit \
--var xp '//elt/following::comment()' \
-a xp -t elem -n p -v 'some new text' \
-a xp -t elem -n p -v 'some other text' \
/tmp/file.xml
What does I miss to use variable?