我想知道在不影响肉类产品节点的情况下仅删除蔬菜产品节点的最佳方法是哪种。
<Product>
<Type>Meat</Type>
<Object>
<ID>1</ID>
</Object>
</Product>
<Product>
<Type>Vegetables</Type>
<Object>
<ID>1</ID>
</Object>
</Product>
我想知道在不影响肉类产品节点的情况下仅删除蔬菜产品节点的最佳方法是哪种。
<Product>
<Type>Meat</Type>
<Object>
<ID>1</ID>
</Object>
</Product>
<Product>
<Type>Vegetables</Type>
<Object>
<ID>1</ID>
</Object>
</Product>
Wrap your snippet in a top level tag to create a well-formed XML file. Then use e.g. the command line tool xmlstarlet to modify the XML.
For your case the staep by step instructions:
Add a toplevel tag, say, MyTopLevelTag
, so that your XML files looks like :
Meat 1 Vegetables 1
Save it as how-to-remove-vegetables_wrapped.xml
Install xmlstarlet. (Under OSX you can use homebrew).
Type in your shell:
xml ed -d "/MyTopLevelTag/Product[Type = 'Vegetables']" how-to-remove-vegetables_wrapped.xml
The magic is in the XPath /MyTopLevelTag/Product[Type = 'Vegetables']
: You select the Product
tag, but you restrict it to its children with the type
value of 'Vegetables'
.