简体   繁体   中英

Add attribute that does not yet exist in xml element with xmlstarlet

I have an xml file beginning with

<?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"
>

I need to add a ios-CFBundleVersion="de.test" attribute so it should look like this:

<?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"
>

I've tried it with xmlstarlet:

xmlstarlet edit \
  -O \
  --inplace \
  --insert "widget" \
  --type attr \
  -n ios-CFBundleVersion \
  -v de.test \
  config.xml 

but nothing happens in my file config.xml . What would be the correct xmlstarlet command here?

You forgot that the widget element has a namespace, hence your XPath didn't match. So define one with the -N global option and your command starts to work:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM