is it possible to check by xmllint if xml include the follwing tags:

  1. Name

  2. uniq_String

  3. Version

example of xml with three tags:

<?xml version="1.0" encoding="UTF-8" ?>
<!-- tasks configuration file -->
   <uniq_String>destry_srorage_luns run once at day</uniq_String>

if not by using xmllint

what the other alternative to check tags are defined in xml with awk/sed/perl oneliner?

expected output in case xml include both tags - Name , uniq_String , Version


expected output in case xml NOT include both tags - Name , uniq_String , Version

  • 1
    do you have a DTD for this XML file? xmllint can check if an XML file is valid according to its DTD (which is the "correct" way to check if an XML file is valid. All other methods are essentially hacks of varying degrees of quality - RomanPerekhrest's xmlstarlet version is a very high-quality hack because it uses a tool that parses and queries XML. xmlstarlet can also validate XML. A few simple regpexp matches would be low-quality). – cas Jan 18 '18 at 7:36

Xmlstarlet solution:

if xmlstarlet sel -Q -t -c "//*[Name and uniq_String and Version]" input.xml; then
    echo "XML_VALID=TRUE"
    echo "XML_VALID=FALSE"


xmllint alternative solution:

if xmllint --xpath "//*[Name and uniq_String and Version]" input.xml &>/dev/null; then
    echo "XML_VALID=TRUE"
    echo "XML_VALID=FALSE"
  • ok fine , but in case - Xmlstarlet not installed on my Linux do you have alternative solution with awk/sed/perl ? – yael Jan 18 '18 at 7:55
  • @yael, see my update – RomanPerekhrest Jan 18 '18 at 8:11
  • what a great solution you are really brilliant – yael Jan 18 '18 at 8:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.