Help with XML tag value extraction based on condition

sample xml file part

<?xml version="1.0" encoding="UTF-8"?><ContractWorkspace xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" _LoadId="export_AJ6iAFmh+pQHq1" xsi:noNamespaceSchemaLocation="ContractWorkspace.xsd">
  <_LocalId>CW2218471</_LocalId>
  <Active>true</Active>
  <ActualTemplateObject _LoadId="export_AJ6iAFmhclFKgb" _Logical="true" class="ariba.collaborate.contracts.ContractWorkspace" ref="true">
    <Workspace>/Templates/Contract Templates/Contract Workspace Professional/OPE Workflow#3/</Workspace>
  </ActualTemplateObject>
  <AgreementPath>/tsoqrqut.4w25m5i</AgreementPath>
  <Alert>Gray</Alert>
  <AllowAdhocSpend>false</AllowAdhocSpend>
  <Amount>$15,000.00 USD</Amount>

Hi ,
I want to search for "ContractWorkspace.xsd" in the in the first line of the xml file and print the value "CW2218471" from the tag <_LocalId> along with value "True" separated by comma delimiter. If ContractWorkspace.xsd does not exist in the first line of the xml file , I want to still print whatever value exists in corresponding <_LocalId> tag along with value "False" separated by comma delimiter. Can you please help me?

for eg: sample output expected if a match is found for string "ContractWorkspace.xsd" in the first line of xml file
=> CW2218471,True

sample output expected if a match is not found for string "ContractWorkspace.xsd" in the first line of xml file
=> value from <_LocalId> tag ,False

Hello paul1234,

Could you please try following and let me know if this helps you.

awk -F"[><]" '/ContractWorkspace.xsd/{A=1;next} A && /<_LocalId>/{val=$3;next} /<Active>/ && A{print val",True";A="";next} /<Active>/ && !A{print val",False"}'   Input_file

Thanks,
R. Singh

Try:

awk '/ContractWorkspace\.xsd/{f=1} $1=="_LocalId"{print $2 (f?",True":",False")}' RS=\< FS=\> file
1 Like

Thank you :slight_smile: