1

我想知道在不影响肉类产品节点的情况下仅删除蔬菜产品节点的最佳方法是哪种。

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

1 回答 1

0

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:

  1. 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

  1. Install xmlstarlet. (Under OSX you can use homebrew).

  2. 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'.

于 2014-02-02T07:41:31.757 回答