如何使用 xmlstarlet 在另一个元素下插入新元素? [英] How to insert a new element under another with xmlstarlet?
本文介绍了如何使用 xmlstarlet 在另一个元素下插入新元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$ vim test.xml
<?xml version="1.0" encoding="UTF-8" ?>
<config>
</config>
$ xmlstarlet ed -i "/config" -t elem -n "sub" -v "" test.xml
<?xml version="1.0" encoding="UTF-8"?>
<sub></sub>
<config>
</config>
但我希望 sub 成为 config 的子级.我应该如何更改 -i 的 xpath 参数?
But I wanted sub to be a child of config. How should I change the xpath parameter of -i?
奖励:是否可以使用属性直接插入孩子,甚至将其设置为一个值?类似的东西:
BONUS: Is it possible to insert the child directly with an attribute and even have it set to a value? Something like:
$ xmlstarlet ed -i "/config" -t elem -n "sub" -v "" -a attr -n "class" -v "com.foo" test.xml
推荐答案
使用-s
(或--subnode
)代替-i
.关于奖励,您不能直接插入带有属性的元素,但是由于每个编辑操作都是按顺序执行的,因此插入一个元素然后添加一个属性:
Use -s
(or --subnode
) instead of -i
. Regarding the bonus, you can't insert an element with an attribute directly but since every edit operation is performed in sequence, to insert an element and then add an attribute:
> xml ed -s /config -t elem -n sub -v "" -i /config/sub -t attr -n class -v com.foo test.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<sub class="com.foo"></sub></config>
这篇关于如何使用 xmlstarlet 在另一个元素下插入新元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文