Search code examples
xmlstarlet

xmlstarlet delete element if string found tag


I need to delete entire cellNote element below if epm_default_cloud_admin is found in any tag, tried different things with xmlstarlet and it won't happen, can you help?

<cell>
    <cellNote>
        <DIM1>Actual</DIM1>
        <author>epm_default_cloud_admin</author>
        <modified>2016-11-16 08:28:38.0</modified>
        <title/>
    </cellNote>
    <cellNote>
        <DIM1>Actual</DIM1>
        <contents>Variance in meals is due to Annual Sales Conference</contents>
        <author>Frank</author>
        <modified>2016-12-23 20:10:13.0</modified>
        <title/>
    </cellNote>
    <cell>

A few things I tried below:

xmlstarlet ed -a "/cell/cellNote" --type elem -n string -v "epm_default_cloud_admin"
xmlstarlet ed -d '/cell/cellNote/author[. = 'epm_default_cloud_admin']' 

Solution

  • At first, to deal with a valid xml - ensure that cell tag has both opening and closing tag (your input contain both as opening <cell>).

    xmlstarlet solution:

    xmlstarlet ed -d "//cellNote[*[contains(text(),'epm_default_cloud_admin')]]" input.xml
    

    The output:

    <?xml version="1.0"?>
    <cell>
      <cellNote>
        <DIM1>Actual</DIM1>
        <contents>Variance in meals is due to Annual Sales Conference</contents>
        <author>Frank</author>
        <modified>2016-12-23 20:10:13.0</modified>
        <title/>
      </cellNote>
    </cell>