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"> 

    1 Answer 1

    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.

    4
    • 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)CommentedMar 2, 2018 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
      CommentedMar 2, 2018 at 9:55
    • ok i try and let you knowCommentedMar 2, 2018 at 10:37
    • we cannot install the upgraded switch due to limitation. can you please to extarct using the string? Oracle suggests the sameCommentedMar 12, 2018 at 11:44

    You must log in to answer this question.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.