我想知道在不影响肉类产品节点的情况下,哪种方法只能删除蔬菜产品节点。
<Product>
<Type>Meat</Type>
<Object>
<ID>1</ID>
</Object>
</Product>
<Product>
<Type>Vegetables</Type>
<Object>
<ID>1</ID>
</Object>
</Product>
答案 0 :(得分:0)
将您的代码段包装在顶级标记中,以创建格式正确的XML文件。然后用例如命令行工具xmlstarlet来修改XML。
对于您的情况,按步骤说明:
添加一个顶级标记,例如MyTopLevelTag
,以便您的XML文件如下所示:
肉 1 蔬菜 1
将其另存为how-to-remove-vegetables_wrapped.xml
安装xmlstarlet。 (在OSX下,您可以使用homebrew)。
输入你的shell:
xml ed -d“/ MyTopLevelTag / Product [Type ='Vegetables']”how-to-remove-vegetables_wrapped.xml
魔法在XPath中/MyTopLevelTag/Product[Type = 'Vegetables']
:您选择了Product
标记,但是将其限制为type
值为'Vegetables'
的子标记。