Search code examples
xmlxmlstarlet

How to modify an XML file in-place with XmlStarlet when there is xmlns attributes


I'm trying to modify the following XML file in-place;

<widget id="" android-version="" ios-version="" version="" xmlns="http://www.w3.org/ns/widgets" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:cdv="http://cordova.apache.org/ns/1.0" xmlns:tools="http://schemas.android.com/tools">
...
</widget>

Particularly trying to modify the values of id android-version ios-version and version attributes.

I'm using the following CMD to modify the empty string value of id for example passing a value to it from the command line with $1;

xmlstarlet edit --inplace \
--update "//widget/@id" \
--value $1 config.xml

This is working fine when I remove the 4 xmlns attributes. I tried to specify these as namespaces with -N but still couldn't modify. I want to be able to modify the values mentioned above without removing xmlns attributes.

What am I missing here? Thanks.


Solution

  • It seems that I was making a mistake when specifying the namespace. Using the following format works;

    xmlstarlet edit --inplace -N a="http://www.w3.org/ns/widgets" \
    --update "//a:widget/@id" \
    --value $1 config.xml
    

    Note: I'm using --inplace to modify the file in-place without appending.