-4

Sorry for the blunt question, need some quick tips on how to pattern match a block in an xml doc.

The xml looks like this

<?xml version="1.0" encoding="UTF-8"?>
<NameSet xmlns="http://soap.sforce.com/2006/04/metadata">
   <applicationVisibilities>
        <application>standard__Service</application>
        <visible>false</visible>
    </applicationVisibilities>
    <applicationVisibilities>
        <application>standard__ServiceConsole</application>
        <visible>false</visible>
    </applicationVisibilities>
    <classAccesses>
        <apexClass>APAC_AddCampaignMembersController</apexClass>
        <enabled>false</enabled>
    </classAccesses>
    <classAccesses>
        <apexClass>APAC_CampaignLogicRedirectClassTestClass</apexClass>
        <enabled>false</enabled>
    </classAccesses>
    <classAccesses>
        <apexClass>APAC_CampaignLogicUtilitiesTestClass</apexClass>
        <enabled>false</enabled>
    </classAccesses>
    <classAccesses>
        <apexClass>APAC_CampaignPropertyTriggerHelper</apexClass>
        <enabled>false</enabled>
    </classAccesses>
    <classAccesses>
        <apexClass>APAC_ConvertEnquiryController</apexClass>
        <enabled>false</enabled>
    </classAccesses>
    <classAccesses>
        <apexClass>APAC_CreateSpaceAssController</apexClass>
        <enabled>false</enabled>
    </classAccesses>
</NameSet>

i want to match this block

<classAccesses>
    <apexClass>APAC_CampaignLogicUtilitiesTestClass</apexClass>
    <enabled>false</enabled>
</classAccesses>

So as it manipulate the xml doc. via sed and remove this block. Currently i can do this,

find . -name *.xml | xargs sed -ri 's/<.*?>APAC_CampaignLogicUtilitiesTestClass<.*?>//g'

but this only removes the line

<apexClass>APAC_CampaignLogicUtilitiesTestClass</apexClass>

And not the block. What will the regex to match this entire block and use in my sed command?

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
OK999
  • 1,353
  • 2
  • 19
  • 39
  • 1
    [Stop trying to parse XML with a regex](https://stackoverflow.com/a/1732454/62576) and use a DOM parser instead. – Ken White Jun 14 '18 at 02:07

1 Answers1

3

Don't use regular expressions to parse XML. Use an XML parser. is one: Once you fix your xml, you can do

xmlstarlet ed -d '//_:classAccesses[_:apexClass = "APAC_CampaignLogicUtilitiesTestClass"]' file.xml 
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • This is exactly what i was looking for. But what were you meaning when you said, "Once you fix your xml"? – OK999 Jun 14 '18 at 15:30
  • 1
    `NameSet` has no opening tag. Look at the 2nd line of the XML you posted – glenn jackman Jun 14 '18 at 15:35
  • Thanks again for pointing that out. i have corrected it. I messed that up while replacing the actual word in there. Btw, what is the pattern used here? where can i read more on this. – OK999 Jun 14 '18 at 16:23
  • I'm not sure I can explain well : the XML has an xmlns attribute but no specified prefix. xmlstarlet uses underscore as the default prefix – glenn jackman Jun 14 '18 at 18:07