Search code examples
xmlxpathxml-parsingxmlstarlet

xmlstarlet Search Attribute


XML Example:

<?xml version="1.0" encoding="UTF-8"?>
<profile>
<section name="Vision">
<key name="Name" value="BBBB"/>
<key name="Name_Remark" value="GGGG"/>
<key name="Position" value="30"/>
</section>
</profile>

How I can get value with xmlstarlet where name is "Position". Now is 30.

Thank you.


Solution

  • You can use this command line...

    xmlstarlet sel -t -v "/profile/section/key[@name='Position']/@value" -n input.xml
    

    The sel command tells xmlstarlet to select.

    The -t option tells xmlstarlet the options following -t are for templates.

    The -v tells xmlstarlet to print the value of the XPath expression.

    The -n tells xmlstarlet to print a new line. (Not completely necessary.)

    All of this can be found by running xmlstarlet sel --help from the command line.