
マニュアルに従って:
xmlstarlet edit --help
--var
XPath式を変数として宣言するために使用できることを読むことができます。
mocファイルの生成:
cat<<EOF > /tmp/file.xml
<root>
<elt>x</elt>
<!-- comment -->
<elt>y</elt>
<!-- other comment -->
</root>
EOF
これは変数なしで機能します。
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
これは変数を使わずに編集されました。
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
変数を使用すると何が欠けていますか?
答え1
'$xp'
変数を参照するには:
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