我正在尝试编辑一个XML文件,看起来像这样:
<Item>
<rasd:AllocationUnits>hertz * 10^6</rasd:AllocationUnits>
<rasd:Description>Number of Virtual CPUs</rasd:Description>
<rasd:ElementName>8 virtual CPU(s)</rasd:ElementName>
<rasd:InstanceID>1</rasd:InstanceID>
<rasd:Reservation>18000</rasd:Reservation>
<rasd:ResourceType>3</rasd:ResourceType>
<rasd:VirtualQuantity>8</rasd:VirtualQuantity>
<vmw:CoresPerSocket ovf:required="false">8</vmw:CoresPerSocket>
</Item>
<Item>
<rasd:AllocationUnits>byte * 2^20</rasd:AllocationUnits>
<rasd:Description>Memory Size</rasd:Description>
<rasd:ElementName>8192MB of memory</rasd:ElementName>
<rasd:InstanceID>2</rasd:InstanceID>
<rasd:Reservation>8000</rasd:Reservation>
<rasd:ResourceType>4</rasd:ResourceType>
<rasd:VirtualQuantity>8192</rasd:VirtualQuantity>
</Item>
<Item>
<rasd:Address>0</rasd:Address>
<rasd:Description>SCSI Controller</rasd:Description>
<rasd:ElementName>SCSI Controller 0</rasd:ElementName>
<rasd:InstanceID>3</rasd:InstanceID>
<rasd:ResourceSubType>VirtualSCSI</rasd:ResourceSubType>
<rasd:ResourceType>6</rasd:ResourceType>
</Item>
<Item>
<rasd:Address>1</rasd:Address>
<rasd:Description>IDE Controller</rasd:Description>
<rasd:ElementName>VirtualIDEController 1</rasd:ElementName>
<rasd:InstanceID>4</rasd:InstanceID>
<rasd:ResourceType>5</rasd:ResourceType>
</Item>
在包含:
的<item>
元素内部<rasd:Description>Memory Size</rasd:Description>
我想编辑(或添加,如果它不存在)标题为<rasd:Reservation>
的元素
我可以处理条件以确定元素是否存在或需要更改,但是当其中一些元素具有重复名称时,我在弄清楚如何让xmlstarlet选择正确的元素时遇到了问题。
假设您的XML文件看起来像
<Items xmlns:rasd="foo" xmlns:vmw="bar" xmlns:ovf="baz">
// your content here
</Items>
然后你可以使用do
xmlstarlet ed -N rasd=foo
-u '//Item[rasd:Description = "Memory Size"]/rasd:Reservation'
-v 12345
file.xml