【问题标题】:Add attribute that does not yet exist in xml element with xmlstarlet使用 xmlstarlet 添加 xml 元素中尚不存在的属性
【发布时间】:2018-05-24 12:21:53
【问题描述】:

我有一个以

开头的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 命令是什么?

【问题讨论】:

    标签: xml xmlstarlet


    【解决方案1】:

    您忘记了 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-19
      • 2014-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-28
      • 1970-01-01
      相关资源
      最近更新 更多