read xml tag attribute and store it in variable

Hi,

How to read xml tag attributes and store into variable in shell script?

Thanks,
Swetha

Show an example input and desired output. Use [ CODE ] tags when display code, data or logs please :wink:

this is xml from which i want to read attribute 'transaction' and 'document_mode' and put it in some variables in shell script
Xml :
<PURCHASE_10 partner="food" version="1.50" timestamp="2009-03-10T09:56:55" transaction="PURCHASEORDER" document_mode="abc">
</PURCHASE_10>

Here is the solution

tranOrder=$(sed '/transaction/s/\(.*transaction=\)\(.\)/\2/' fileName|awk -F\" '{print $2}')
docMode=$(sed '/document_mode/s/\(.*document_mode=\)\(.
\)/\2/' fileName|awk -F\" '{print $2}')
echo $tranOrder $docMode

while read line
do
trans=$(echo $line | sed -n 's/.*\" transaction=\"\([^"]*\)\" .*/\1/p')
doc=$(echo $line | sed ' -n s/.*\" document_mode=\"\([^"]*\)\".*/\1/p')
echo "$trans"
echo "$docs"
done < xmlfile

-Devaraj Takhellambam

Try...

$ eval $(tr '[< >]' '\n' < file1 | egrep 'transaction|document_mode')
$ echo $transaction $document_mode
PURCHASEORDER abc