Question

I´m using the xmllint Linux command (xmllint description) and I want to capture the error code to use it inside a script. How can I do it?

Thanks

Was it helpful?

Solution

You can doing this :

if xmllint --xpath '/my/path' file.xml; then
    echo "success"
else
    echo >&2 "error"
    exit 1
fi

If you don't have --xpath switch :

file=/path/to/file
xpath='/foo/bar'
result="$(echo "cat $xpath" | xmllint --shell "$file")"

if [ $(echo "$result" | wc -l) -gt 2 ]; then
    echo "success"
else
    echo >&2 "error"
    exit 1
fi

OTHER TIPS

Or if you have schema that you want to validate your xml file with you can do this: xmllint --noout --schema your_xsd_file your_xml_file 2>myxmlval.out

myxmlval.out will be a file that capture error that comes out from xmllint schema validation

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top