7

I'm working on a script which deals with an xml file. I'd like to update an attribute value in this xml file with xmlstarlet but it doesn't work.

Here is a sample of the xml file:

<?xml version="1.0" encoding="UTF-8"?>
<channel name="xfce4-keyboard-shortcuts" version="1.0">
  <property name="commands" type="empty">
    <property name="default" type="empty">
      <property name="&lt;Alt&gt;F2" type="empty"/>
      <property name="&lt;Control&gt;&lt;Alt&gt;Delete" type="empty"/>
      <property name="XF86Display" type="string" value="xrandr --output LVDS --auto --output VGA-0 --mode 1680x1050_60.00 --right-of LVDS"/>
    </property>
    <property name="custom" type="empty">
      <property name="&lt;Alt&gt;F2" type="string" value="xfrun4"/>
      <property name="&lt;Control&gt;&lt;Alt&gt;Delete" type="string" value="xflock4"/>
      <property name="XF86Display" type="string" value="xrandr --output LVDS --auto --output VGA-0 --mode 1680x1050_60.00 --right-of LVDS"/>
      <property name="override" type="bool" value="true"/>
    </property>
  </property>
</channel>

And here is the command to update the property with the name "XF86Display" in the custom property node.

xmlstarlet edit \
  --update "/xml/channel[@name=xfce4-keyboard-shortcuts]/property[@name=commands]/property[@name=custom]/property[@name=XF86Display]/@value" \
  --value "test" xfce4-keyboard-shortcuts.xml

This output is strictly the same to the input...

Thank you

Thor
  • 45,082
  • 11
  • 119
  • 130
renard
  • 1,368
  • 6
  • 20
  • 40

2 Answers2

15

This works for me (the root is <channel>; attribute values quoted):

xmlstarlet edit \
  --update "/channel[@name='xfce4-keyboard-shortcuts']/property[@name='commands']/property[@name='custom']/property[@name='XF86Display']/@value" \
  --value  "test" xfce4-keyboard-shortcuts.xml

Or simpler:

xmlstarlet edit \
  --update "//property[@name='XF86Display']/@value" \
  --value "test" xfce4-keyboard-shortcuts.xml
Thor
  • 45,082
  • 11
  • 119
  • 130
mzjn
  • 48,958
  • 13
  • 128
  • 248
  • HI, i'm using this in order to automatically change the version in a ionic app, I need to change `widget/@version` I use `xmlstarlet ed -u 'widget/@version' -v '1.0.10' config.xml` but the output came with no changes – Ernesto Alfonso Jul 14 '20 at 08:44
  • 1
    @ErnestoAlfonso: please ask a new question. – mzjn Jul 14 '20 at 08:46
1

The proper way (will update settings on the fly)

xfconf-query -c xfce4-keyboard-shortcuts -p /commands/default/XF86Display -s 'test'
admirabilis
  • 2,290
  • 2
  • 18
  • 33