Reputation: 51
I have this xml file, and i am trying to use xmllint or grep to get an out put like this
availStart="2024-05-24T05:00:30" availId="811220455" q1:campaignIdRef=180398
availStart="2024-05-24T05:13:41" availId="811220484" q2:campaignIdRef=179163
availStart="2024-05-24T05:13:41" availId="811220484" q2:campaignIdRef=179166
availStart="2024-05-24T05:13:41" availId="811220484" q2:campaignIdRef=179164
availStart="2024-05-24T05:13:41" availId="811220484" q2:campaignIdRef=179165
The XML file is below, i tried using while read Line method and some grep commands but not getting the full capture. Also xmllint im struggling to use the xpath or path option. can anyone help?
<?xml version="1.0" encoding="utf-8"?>
<Schedule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:scte:118:version01" level="0" revision="1" zoneName="test" networkName="XXX" schemaVersion="urn:scte:118:version01" begDateTime="2024-05-24T06:00:00" broadcastDate="20240524" endDateTime="2024-05-25T05:59:59">
<Window windowStart="2024-05-24T04:00:30" windowDuration="02000000">
<Avail xmlns:q1="urn:nds:dynamic:ruleManagerICD:01:01" xsi:type="q1:NDSAvailType" availStart="2024-05-24T05:00:30" availInWindow="1" availId="811220455" eventKey="811220455" durationFrames="0" durationSeconds="15" framesPerSecond="25Frames" breakId="1">
<Spot xsi:type="q1:SpotRulesType" spotId="1" length="00001500" positionInAvail="1" trafficId="312999" eventType="LOI" lengthFrames="0">
<q1:substitutionOptionSlot>
<q1:adInSpot>
<q1:campaignRef>
<q1:campaignIdRef>180398</q1:campaignIdRef>
</q1:campaignRef>
</q1:adInSpot>
</q1:substitutionOptionSlot>
</Spot>
<q1:inactivityTime primaryDevice="7200" secondaryDevice="5400"/>
</Avail>
</Window>
<Window windowStart="2024-05-24T04:13:41" windowDuration="02000000">
<Avail xmlns:q2="urn:nds:dynamic:ruleManagerICD:01:01" xsi:type="q2:NDSAvailType" availStart="2024-05-24T05:13:41" availInWindow="1" availId="811220484" eventKey="811220484" durationFrames="0" durationSeconds="10" framesPerSecond="25Frames" breakId="2">
<Spot xsi:type="q2:SpotRulesType" spotId="1" length="00001000" positionInAvail="1" trafficId="312999" eventType="LOI" lengthFrames="0">
<q2:substitutionOptionSlot>
<q2:adInSpot>
<q2:campaignRef>
<q2:campaignIdRef>179163</q2:campaignIdRef>
</q2:campaignRef>
<q2:campaignRef>
<q2:campaignIdRef>179166</q2:campaignIdRef>
</q2:campaignRef>
<q2:campaignRef>
<q2:campaignIdRef>179164</q2:campaignIdRef>
</q2:campaignRef>
<q2:campaignRef>
<q2:campaignIdRef>179165</q2:campaignIdRef>
</q2:campaignRef>
</q2:adInSpot>
</q2:substitutionOptionSlot>
</Spot>
<q2:inactivityTime primaryDevice="7200" secondaryDevice="5400"/>
</Avail>
</Window>
</Schedule>
I tried this but i am not getting any value in $avail varibale. only the first variable $time is getting filled
while read LINE; do time=
grep -io -P 'availstart=.{20}' && avail=
grep -io -P 'availId=.{11}';echo -e "\n$time and $avail";done < sample.xml
Upvotes: 1
Views: 109
Reputation: 3423
Please don't use regex to parse XML, but use a dedicated parser like xidel instead:
$ xidel -s sample.xml -e '
for $x in //Avail
for $y in $x//*[local-name()="campaignIdRef"]
return
`availStart="{$x/@availStart}" availId="{$x/@availId}" {name($y)}={$y}`
'
availStart="2024-05-24T05:00:30" availId="811220455" q1:campaignIdRef=180398
availStart="2024-05-24T05:13:41" availId="811220484" q2:campaignIdRef=179163
availStart="2024-05-24T05:13:41" availId="811220484" q2:campaignIdRef=179166
availStart="2024-05-24T05:13:41" availId="811220484" q2:campaignIdRef=179164
availStart="2024-05-24T05:13:41" availId="811220484" q2:campaignIdRef=179165
Upvotes: 1
Reputation: 12662
xmllint
+ xpath
is a good option to do the job
#!/bin/bash
line=''
prevline=''
while IFS='=' read attr value;do
#echo "$attr | $value"
if [ -n "$value" ];then
line+="$attr="'"'"$value"'"'
if [ "$attr" = ' availId' ];then
prevline="$line"
fi
else
echo -e "${prevline:1} q2:campaignIdRef=$attr"
line=''
fi
done < <(xmllint --xpath '//@availStart | //@availId | //*[local-name()="Avail"]//*[local-name()="campaignIdRef"]/text()' tmp2.xml | tr -d '"')
Result
availStart="2024-05-24T05:00:30" availId="811220455" q2:campaignIdRef=180398
availStart="2024-05-24T05:13:41" availId="811220484" q2:campaignIdRef=179163
availStart="2024-05-24T05:13:41" availId="811220484" q2:campaignIdRef=179166
availStart="2024-05-24T05:13:41" availId="811220484" q2:campaignIdRef=179164
availStart="2024-05-24T05:13:41" availId="811220484" q2:campaignIdRef=179165
Upvotes: 0