使用xmlstarlet更新属性值

时间:2013-04-13 06:57:29

标签: xml xml-namespaces xml-documentation xmlstarlet

我想使用xmlstarlet修改xml文件的值(我知道如何使用grep或sed修改行,但我想学习xmlstarlet)。

我正在查看官方文档(http://xmlstar.sourceforge.net/doc/UG/xmlstarlet-ug.html),但我对xml结构完全不满意。

我想在这里修改值:

<ExtraDataItem name="GUI/UpdateDate" value=""/>

这是我尝试过的命令,但输出与输入值相同:

xml edit --update "//ExtraDataItem[@name='GUI/UpdateDate']/@value" --value "never" virtualbox.xml

这是XML文件:

<?xml version="1.0"?>
<!--
** DO NOT EDIT THIS FILE.
** If you make changes to this file while any VirtualBox related application
** is running, your changes will be overwritten later, without taking effect.
** Use VBoxManage or the VirtualBox Manager GUI to make changes.
-->
<VirtualBox xmlns="http://www.innotek.de/VirtualBox-settings" version="1.12-windows">
  <Global>
    <ExtraData>
      <ExtraDataItem name="GUI/DetailsPageBoxes" value="general,system,preview,display,storage,audio,network,usb,sharedFolders,description"/>
      <ExtraDataItem name="GUI/GroupDefinitions/" value="m=3fa2f128-b094-40c1-bec6-6580b0ac597a,m=65c3158a-330e-4456-b770-768dfecc788f"/>
      <ExtraDataItem name="GUI/HostScreenSaverDisabled" value="false"/>
      <ExtraDataItem name="GUI/Input/AutoCapture" value="true"/>
      <ExtraDataItem name="GUI/Input/HostKeyCombination" value="163"/>
      <ExtraDataItem name="GUI/LastItemSelected" value="m=Windows XP SP3 (x86)"/>
      <ExtraDataItem name="GUI/LastWindowPosition" value="575,245,770,550"/>
      <ExtraDataItem name="GUI/SplitterSizes" value="289,477"/>
      <ExtraDataItem name="GUI/SuppressMessages" value=",remindAboutInaccessibleMedia,cannotAccessUSB"/>
      <ExtraDataItem name="GUI/TrayIcon/Enabled" value="false"/>
      <ExtraDataItem name="GUI/UpdateCheckCount" value="3"/>
      <ExtraDataItem name="GUI/UpdateDate" value=""/>
    </ExtraData>
    <MachineRegistry>
      <MachineEntry uuid="{65c3158a-330e-4456-b770-768dfecc788f}" src="C:\Users\Administrador\VirtualBox VMs\Windows 7 x64\Windows 7 x64.vbox"/>
      <MachineEntry uuid="{3fa2f128-b094-40c1-bec6-6580b0ac597a}" src="C:\Users\Administrador\VirtualBox VMs\Windows XP SP3 (x86)\Windows XP SP3 (x86).vbox"/>
    </MachineRegistry>
    <MediaRegistry>
      <HardDisks/>
      <DVDImages/>
      <FloppyImages/>
    </MediaRegistry>
    <NetserviceRegistry>
      <DHCPServers>
        <DHCPServer networkName="HostInterfaceNetworking-VirtualBox Host-Only Ethernet Adapter" IPAddress="192.168.56.100" networkMask="255.255.255.0" lowerIP="192.168.56.101" upperIP="192.168.56.254" enabled="1"/>
      </DHCPServers>
    </NetserviceRegistry>
    <SystemProperties defaultMachineFolder="C:\Users\Administrador\VirtualBox VMs" defaultHardDiskFormat="VDI" VRDEAuthLibrary="VBoxAuth" webServiceAuthLibrary="VBoxAuth" LogHistoryCount="3"/>
    <USBDeviceFilters/>
  </Global>
</VirtualBox>

1 个答案:

答案 0 :(得分:1)

解决方案是选择命名空间:

xml ed -N N="http://www.innotek.de/VirtualBox-settings" --update "//N:ExtraDataItem[@name='GUI/UpdateDate']/@value" --value "never" virtualbox.xml