.. index:: single: validate script single: scripts; validate validate -- Validate XML ======================== The ``validate`` script can check if an :ref:`xml_source` conforms to an XML schema. It supports the following XML schema languages. .. index:: single: validate script; XSD single: XSD single: XML Schema Definition single: XML Schema XSD --- .. program:: validate .. option:: -x , --xsd Use the ``--xsd`` option to validate an XML source with an XSD [#]_ file: .. code-block:: bash validate -x schema.xsd source.xml .. index:: single: validate script; DTD single: DTD single: Document Type Definition DTD --- .. program:: validate .. option:: -d , --dtd Validate an XML source with a DTD [#]_ file with the ``--dtd`` option: .. code-block:: bash validate -d doctype.dtd source.xml .. index:: single: validate script; RELAX NG single: RELAX NG RELAX NG -------- .. program:: validate .. option:: -r , --relaxng The ``--relaxng`` option validates an XML source with a RELAX NG [#]_ file: .. code-block:: bash validate -r relaxng.rng source.xml Options ------- ``validate`` can be used with the following command-line options: .. code-block:: console $ validate --help usage: validate [-h] [-V] (-x XSD_SOURCE | -d DTD_SOURCE | -r RELAXNG_SOURCE) [-f | -F] [xml_source [xml_source ...]] Validate XML source with XSD, DTD or RELAX NG. positional arguments: xml_source XML source (file, , http://...) optional arguments: -h, --help show this help message and exit -V, --version show program's version number and exit -x XSD_SOURCE, --xsd XSD_SOURCE XML Schema Definition (XSD) source -d DTD_SOURCE, --dtd DTD_SOURCE Document Type Definition (DTD) source -r RELAXNG_SOURCE, --relaxng RELAXNG_SOURCE RELAX NG source -f, -l, --validated-files only the names of validated XML files are written to standard output -F, -L, --invalidated-files only the names of invalidated XML files are written to standard output XML Validation -------------- Validate XHTML with the :download:`XHTML 1.0 strict DTD <../examples/dtd/xhtml1-strict.dtd>`: .. code-block:: bash curl -s https://www.webstandards.org/learn/reference/templates/xhtml10s/ | validate -d examples/dtd/xhtml1-strict.dtd Validate XHTML with the :download:`XHTML 1.0 strict XSD <../examples/xsd/xhtml1-strict.xsd>`: .. code-block:: bash curl -s https://www.webstandards.org/learn/reference/templates/xhtml10s/ | validate -x examples/xsd/xhtml1-strict.xsd Validation Errors ----------------- If an :ref:`xml_source` doesn't validate the ``validate`` script will show the reason with some additional information: .. code-block:: bash validate -x TV-Anytime.xsd NED120200816E.xml XML source 'NED120200816E.xml' does not validate line 92, column 0: Element '{urn:tva:metadata:2019}Broadcaster': This element is not expected. Expected is one of ( {urn:tva:metadata:2019}FirstShowing, {urn:tva:metadata:2019}LastShowing, {urn:tva:metadata:2019}Free ). XSD Validation -------------- Validate an XSD file with the :download:`XML Schema schema document <../examples/xsd/XMLSchema.xsd>`: .. code-block:: bash validate -x examples/xsd/XMLSchema.xsd schema_file.xsd Validate the XML Schema 1.1 XSD with the (identical) XML Schema schema document: .. code-block:: bash validate -x examples/xsd/XMLSchema.xsd http://www.w3.org/2009/XMLSchema/XMLSchema.xsd And vice versa: .. code-block:: bash validate -x http://www.w3.org/2009/XMLSchema/XMLSchema.xsd examples/xsd/XMLSchema.xsd Validate the XML Schema XSD with the :download:`DTD for XML Schema <../examples/dtd/XMLSchema.dtd>`: .. code-block:: bash validate -d examples/dtd/XMLSchema.dtd examples/xsd/XMLSchema.xsd Print file names ---------------- .. program:: validate .. option:: -f, -l, --validated-files The ``-f, -l, --validated-files`` command-line option only prints the names of validated XML files. Find XML files that validate: .. code-block:: bash validate -x schema.xsd *.xml -l .. program:: validate .. option:: -F, -L, --invalidated-files The ``-F, -L, --invalidated-files`` command-line option only prints the names of XML files that don't validate. Remove XML files that fail to validate: .. code-block:: bash validate -x schema.xsd *.xml -L | xargs rm .. rubric:: Footnotes .. [#] `XML Schema 1.0 and 1.1 `_ .. [#] `XML Document Type Definition `_ .. [#] `RELAX NG Specification `_