XPathとして変数を使用する「xmlstarletの編集」

XPathとして変数を使用する「xmlstarletの編集」

マニュアルに従って:

xmlstarlet edit --help

--varXPath式を変数として宣言するために使用できることを読むことができます。

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

おすすめ記事