0

I Have a file. where I need to extract dataTemplate name. need a value in datalob=XXPFARCUDO_DATA

<?xml version="1.0" encoding="windows-1252" ?>
<dataTemplate name="XXPFARCUDO_DATA" description="Ctrole de cohnce des clits doeux et conteeux" Version="1.0">
 <parameters>
  <parameter name="P_SOCIETE"               dataType="character"/>
 </parameters>
.
.
.
.
</dataTemplate>

I have code but it is not working

filename='XXPFARCUDO_DATA.xml'
LOBCODE=$(sed -n 's:.*<dataTemplate name="\(.*\)" description=".*:\1:p' "${filename}")
echo " --> ${LOBCODE} "

it result is not correct

 --> XXPFARCUDO_DATA▒role de cohnce des clits doeux et conteeux" Version="1.0">
2

Assuming that the XML is well formed, such as

<?xml version="1.0" encoding="windows-1252"?>
<dataTemplate name="XXPFARCUDO_DATA" description="Ctrole de cohnce des clits doeux et conteeux" Version="1.0">
  <parameters>
    <parameter name="P_SOCIETE" dataType="character"/>
  </parameters>
</dataTemplate>

You would get the value of the attribute name in the dataTemplate node through XMLStarlet like this:

LOBCODE=$( xml sel -t -v '/dataTemplate/@name' "$filename" )

XMLStarlet is sometimes installed as xmlstarlet rather than as xml.

Using xmllint:

LOBCODE=$( xmllint --xpath 'string(/dataTemplate/@name)' "$filename" )

Please, don't try to parse XML using sed. It is error prone and fragile.

  • If I use this LOBCODE=$( xmllint --xpath 'string(/dataTemplate/@name)' "$filename" ) Unknown option --xpath --> Usage : xmllint [options] XMLfiles ... Parse the XML files and output the result of the parsing --version : display the version of the XML library used --debug : dump a debug tree of the in-memory document and more..... and for LOBCODE=$( xml sel -t -v '/dataTemplate/@name' "$filename" ) it gives LPX-00202: could not open "sel" (error 200) – Arun GoWdA Mar 2 '18 at 9:53
  • @ArunGoWdA The first error comes from an older version of xmllint. The second error comes from the fact that xml on your system is not XMLStarlet but some other program. Try xmlstarlet (after making sure that the program is actually installed), or upgrade your xmllint. – Kusalananda Mar 2 '18 at 9:55
  • ok i try and let you know – Arun GoWdA Mar 2 '18 at 10:37
  • we cannot install the upgraded switch due to limitation. can you please to extarct using the string? Oracle suggests the same – Arun GoWdA Mar 12 '18 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.