xmlstarlet update an attribute

牧云@^-^@ 提交于 2019-12-12 18:16:16

问题


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


回答1:


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



回答2:


The proper way (will update settings on the fly)

xfconf-query -c xfce4-keyboard-shortcuts -p /commands/default/XF86Display -s 'test'


来源:https://stackoverflow.com/questions/7837879/xmlstarlet-update-an-attribute

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!