Help with tag value extraction from xml file based on a matching condition

Hi ,
I have a situation where I need to search an xml file for the presence of a tag
<FollowOnFrom> and also , presence of partial part of the following tag <ContractRequest _LoadId and if these 2 exist ,then
extract the value from the following tag <_LocalId> which is
"CW2094139". There can be many such tags in the xml file but I need to extract the first match only. Can you please help ?

part of the xml file

 <Required>AllStates</Required>
 <Status>AllStates</Status>
 </ScheduleSearchFilter>
 </DefaultScheduleSearchFilter>
 <DocumentMinorVersion>0</DocumentMinorVersion>
 <DocumentVersion>1</DocumentVersion>
 <EffectiveDate>2015-04-01T00:00:00Z</EffectiveDate>
 <FollowOnFrom>
 <ContractRequest _LoadId="export_ACSaAFoG9m538H">
 <_LocalId>CW2094139</_LocalId>
 <Active>true</Active>
 <ActualTemplateObject _LoadId="export_ACSaAFoG9J+w8x" _Logical="true" class="ariba.collaborate.contracts.ContractRequest" ref="true">
 <Workspace>/Templates/TMS Folder/IS Contract Templates/Toyota IS RCR Process#70/</Workspace>
 
awk '
/<FollowOnFrom>/ {c=1}
/<ContractRequest .*_LoadId=/ && c==1 {c++}
/<_LocalId>.*<\/_LocalId>/ && c==2 {FS="[<>]"; $0=$0; print $3; exit}
' xml_file
1 Like

Thank You so much for your help. This worked:)