Search code examples
bashcentos6xmllint

xmllint string without --xpath option


I have a xml looking like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer>
    <role>probe</role>
    <ipconfig>yes</ipconfig>
    <hostname>test</hostname>
    <ip>192.168.2.123</ip>
    <gateway>192.168.2.1</gateway>
    <subnet>255.255.254.0</subnet>
</customer>

I have scripts under CentOS7 to get the values with:

role="$(xmllint --xpath "string(//role)" "${xml_path}")"

This works perfectly.

Now I have to use the same under CentOS 6 environment. There is no --xpath support and I can't install it.

What is the easiest and simplest way to achieve the same?


Solution

  • This might work:

    role=$(echo "cat /customer/role/text()" | xmllint --shell file.xml | grep -v '^/')
    

    Output to variable role:

    probe