Search code examples
xmlxmlstarlet

Update attribute that does 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 change the value of the id attribute so it should look like this:

<?xml version='1.0' encoding='utf-8'?>
<widget 
  id="sdf" 
  version="0.0.1" 
  xmlns="http://www.w3.org/ns/widgets" 
  xmlns:cdv="http://cordova.apache.org/ns/1.0"
>

I've tried it with xmlstarlet:

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

but I'm getting a Invalid expression: x:widget@id. What would be the correct xmlstarlet command here?


Solution

  • In XPath attribute names denoted with @ have to be delimited by a slash / from the element.

    So instead of

    --update "x:widget@id"
    

    use

    --update "x:widget/@id"