1

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 -->
<tasks>
   <Name>destry_srorage_luns</Name>
   <uniq_String>destry_srorage_luns run once at day</uniq_String>
    <Version>14.03.23</Version>

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

 XML_VALID=TRUE

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

 XML_VALID=FALSE
1
  • 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
2

Xmlstarlet solution:

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

http://xmlstar.sourceforge.net/doc/UG/xmlstarlet-ug.html#idm47077139652416


xmllint alternative solution:

if xmllint --xpath "//*[Name and uniq_String and Version]" input.xml &>/dev/null; then
    echo "XML_VALID=TRUE"
else 
    echo "XML_VALID=FALSE"
fi
3
  • 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.