如何从命令行中删除父XML节点

时间:2013-04-04 19:54:07

标签: xml xml-parsing

我想知道在不影响肉类产品节点的情况下,哪种方法只能删除蔬菜产品节点。

<Product>
    <Type>Meat</Type>
    <Object>
      <ID>1</ID>
    </Object>
  </Product>
<Product>
    <Type>Vegetables</Type>
    <Object>
      <ID>1</ID>
    </Object>
  </Product>

1 个答案:

答案 0 :(得分:0)

将您的代码段包装在顶级标记中,以创建格式正确的XML文件。然后用例如命令行工具xmlstarlet来修改XML。

对于您的情况,按步骤说明:

  1. 添加一个顶级标记,例如MyTopLevelTag,以便您的XML文件如下所示:

                 肉                    1                              蔬菜                    1                

  2. 将其另存为how-to-remove-vegetables_wrapped.xml

    1. 安装xmlstarlet。 (在OSX下,您可以使用homebrew)。

    2. 输入你的shell:

      xml ed -d“/ MyTopLevelTag / Product [Type ='Vegetables']”how-to-remove-vegetables_wrapped.xml

    3. 魔法在XPath中/MyTopLevelTag/Product[Type = 'Vegetables']:您选择了Product标记,但是将其限制为type值为'Vegetables'的子标记。