Xmlstarlet remove parent elements from multiple documents recursively

You have a bunch of XML documents in separate directories, each of which looks like this:

    <xmldoc>
      <data>data text</data>
      <parent>
        <descendent1>
          <descendent2>motorcyclebaby.com?query</descendent2>
        </descendent1>
      </parent>
    </xmldoc>

      

I want to remove the parent element containing "motorcycle" in descendent2 in all documents. So far I have this, which works for a single document:

   xmlstarlet ed -d "//parent[descendent1/descendent2[contains(text(),'motorcycle')]]" xmldoc.xml

      

I tried this to recursively remove the parent element using find:

    find . -name 'xmldoc.xml' -print0 | xmlstarlet ed -d "//parent[descendent1/descendent2[contains(text(),'motorcycle')]]" xmldoc.xml

      

but got 'could not load external object' xmldoc.xml ''

also tried:

    find . -name "xmldoc.xml" -exec xmlstarlet ed -d "//parent[descendent1/descendent2[contains(text(),'motorcycle')]]" xmldoc.xml {} +

      

+3


source to share


1 answer


It revealed!

    find . -iname "xmldoc.xml" -exec xmlstarlet ed --inplace -d "//parent[descendent1/descendent2[contains(text(),'motorcycle')]]" {} +

      

-name ignore case as my documents were capitalized

// parent [descendent1 / descendent2 [contains ... you must select the entire parent element where the child contains the search string.



- the space needed to make changes to the actual documents, not to print at the terminal.

{} + is required with -exec to make changes to every document listed in the search list.

\ about/

+2


source







All Articles