bash extract xml attribute value using xmllint

13,202

Solution 1

It looks like you call xmllint in wrong way.

xmllint --xpath '//firstpart/step/category/id/info[@key="qua"]/@value' FILE.xml

Result:

value="1" value="2"

Complete script:

#!/bin/bash

str=$(xmllint --xpath '//firstpart/step/category/id/info[@key="qua"]/@value' $1)

entries=($(echo ${str}))
for entry in "${entries[@]}"; do
    result=$(echo $entry | awk -F'[="]' '!/>/{print $(NF-1)}')
    echo "result: $result"
done

May be, it's not better solution, but at least it works :)

Solution 2

Get value of attribute with xmllint:

xmllint --xpath 'string(//firstpart/step[1]/category/id/info/@value)' file.xml

Output:

1
Share:
13,202
passion
Author by

passion

Updated on June 27, 2022

Comments

  • passion
    passion almost 2 years

    I am extracting an attribute value from xml file, but I get an error. I'd like to extract the value for key="qua" in the firstpart element. Here is my script, but below you find the errors:

    #!/bin/bash
    
    myfile=$1
    
    myvar=$(echo 'cat //firstpart/step/category/id/info[@key="qua"]/@value' | xmllint --xpath "$myfile" | awk -F'[="]' '!/>/{print $(NF-1)}')
    
    echo "$myvar"
    

    how my xml file looks like:

    <?xml version='1.0' encoding='UTF-8'?>
    <firstpart>
        <step name="Home">    
            <category name="one">
                <id name="tools">
                    <info key="qua" value="1"/>        
                </id>
            </category>
        </step>
        <step name="Contact">    
            <category name="two">
                <id name="tools">
                    <info key="qua" value="2"/>        
                </id>
            </category>
        </step>
        ...
    </firstpart>
    <secondpart>
        <step name="office">    
            <category name="one">
                <id name="tools">
                    <info key="qua" value="100"/>        
                </id>
            </category>
        </step>
        <step name="Contact">    
            <category name="two">
                <id name="tools">
                    <info key="qua" value="200"/>        
                </id>
            </category>
        </step>
        ...
    </secondpart>
    

    the errors I get:

    awk: run time error: negative field index $-1
        FILENAME="-" FNR=71 NR=71
    
    ./mybash.sh: line 3: $: command not found
    ./mybash.sh: line 4: $: command not found
    
    • Ed Morton
      Ed Morton about 7 years
      Have you tried saving the xmllint output to a file and running awk on that? I assume not since your problem will be extremely obvious if you do.
    • passion
      passion about 7 years
      @EdMorton how can i do it? could you provide an answer please?
    • choroba
      choroba about 7 years
      Instead of piping to awk, redirect to a file. Then run awk on that file and get the same error. Then inspect the file to see the problem.
    • Cyrus
      Cyrus about 7 years
      Your file is not valid XML.
    • passion
      passion about 7 years
      @Cyrus why is it not a valid one? i have excluded the tail of the file and included three dots in the middle to say that they are other stuff there...
    • Cyrus
      Cyrus about 7 years
      What is your desired output? key="qua" or qua?
    • Cyrus
      Cyrus about 7 years
      @passion: An XML has one root node: <foo><firstpart></firstpart><secondpart></secondpart></foo>
  • passion
    passion about 7 years
    how can i get the value only and not the ` value="1"` ? on the the number
  • passion
    passion about 7 years
    I like this solution but since Bor laze was first to answer, so I accept that one as the answer to the question.