3

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?

1 Answer 1

5

Use '$xp' to reference your variable:

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
2
  • Not obvious, an example in the doc could have been interesting, thanks Feb 28 at 9:16
  • 1
    @MévatlavéKraspek It is mentioned in the documentation of the --var option in xmlstarlet select --help, but the same option has a shortened help text in xmlstarlet edit --help. It works the same in select as in edit though.
    – Kusalananda
    Feb 28 at 10:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .