How to verify whether a xml file is valid in sh or bash? How to verify whether a xml file is valid in sh or bash? xml xml

How to verify whether a xml file is valid in sh or bash?


Not directly with bash, but xmllint is fairly widely available.

xmllint --format "${xmlfile}"

This will exit with non-zero status (hint: $? in bash gets you the exit code of the last command) if the XML file is invalid.


XMLStarlet has a validate subcommand. At its simplest, to check for well-formedness:

xmlstarlet val "$filename"

To validate against a DTD:

xmlstarlet val -d "$dtd_filename" "$xml_filename"

To validate against an XSD schema:

xmlstarlet val -s "$xsd_filename" "$xml_filename"

To validate against a RelaxNG schema:

xmlstarlet val -r "$rng_filename" "$xml_filename"

This isn't built into bash -- bash has no built-in XML parser, and validation cannot be performed without one -- but it is widely packaged for modern OS distributions.


XMLStarlet also has subcommands for extracting information from XML files, editing XML files, etc. If you're going to be working with XML from shell scripts, its use is well-advised.


If you want to validate against a RelaxNG schema, which is an alternative grammar to W3C XML schema, you can use Libxml2 (xmllint) but it only supports the RelaxNG XML syntax.

To validate an XML file with Libxml2 against a RelaxNG schema

xmllint --noout --relaxng schema.rng file.xml

It is possible to convert a RelaxNG schema from compact syntax to XML syntaxt with trang. But you may also use Jing to To validate against a RelaxNG XML schema.

With jing installed on your computer, you can validate a file file.xml against a schema schema.relaxng like this :

jing schema.rng file.xml

To use the RelaxNG compact syntax :

jing -c schema.rnc file.xml