![](/img/trans.png)
[英]xmlstarlet: create an attribute if it does not exist and edit it otherwise
[英]Add attribute that does not yet exist in xml element with xmlstarlet
我有一个以。开头的xml文件
<?xml version='1.0' encoding='utf-8'?>
<widget
id="io.ionic.starter"
version="0.0.1"
xmlns="http://www.w3.org/ns/widgets"
xmlns:cdv="http://cordova.apache.org/ns/1.0"
>
我需要添加ios-CFBundleVersion="de.test"
属性,因此它应如下所示:
<?xml version='1.0' encoding='utf-8'?>
<widget
id="io.ionic.starter"
version="0.0.1"
xmlns="http://www.w3.org/ns/widgets"
xmlns:cdv="http://cordova.apache.org/ns/1.0"
ios-CFBundleVersion="de.test"
>
我已经尝试过xmlstarlet了:
xmlstarlet edit \
-O \
--inplace \
--insert "widget" \
--type attr \
-n ios-CFBundleVersion \
-v de.test \
config.xml
但是我的文件config.xml
什么也没有发生。 这里正确的xmlstarlet命令是什么?
您忘记了widget
元素具有名称空间,因此您的XPath不匹配。 因此,使用-N
全局选项定义一个,您的命令将开始工作:
xmlstarlet edit --inplace -O -N x=http://www.w3.org/ns/widgets \
--insert "x:widget" --type attr \
-n "ios-CFBundleVersion" -v "de.test" config.xml
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.