validate – Validate XML¶
The validate
script can check if an XML source conforms to an XML schema.
It supports the following XML schema languages.
XSD¶
-
-x
<xml_schema>
,
--xsd
<xml_schema>
¶
Use the --xsd
option to validate an XML source with an XSD [1] file:
validate -x schema.xsd source.xml
DTD¶
-
-d
<dtd_schema>
,
--dtd
<dtd_schema>
¶
Validate an XML source with a DTD [2] file with the --dtd
option:
validate -d doctype.dtd source.xml
RELAX NG¶
-
-r
<relax_ng_schema>
,
--relaxng
<relax_ng_schema>
¶
The --relaxng
option validates an XML source with a RELAX NG [3] file:
validate -r relaxng.rng source.xml
Options¶
validate
can be used with the following command-line options:
$ 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, <stdin>, 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
XHTML 1.0 strict DTD
:
curl -s https://www.webstandards.org/learn/reference/templates/xhtml10s/ | validate -d examples/dtd/xhtml1-strict.dtd
Validate XHTML with the
XHTML 1.0 strict XSD
:
curl -s https://www.webstandards.org/learn/reference/templates/xhtml10s/ | validate -x examples/xsd/xhtml1-strict.xsd
Validation Errors¶
If an XML source doesn’t validate the validate
script will show the
reason with some additional information:
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
XML Schema schema document
:
validate -x examples/xsd/XMLSchema.xsd schema_file.xsd
Validate the XML Schema 1.1 XSD with the (identical) XML Schema schema document:
validate -x examples/xsd/XMLSchema.xsd http://www.w3.org/2009/XMLSchema/XMLSchema.xsd
And vice versa:
validate -x http://www.w3.org/2009/XMLSchema/XMLSchema.xsd examples/xsd/XMLSchema.xsd
Validate the XML Schema XSD with the
DTD for XML Schema
:
validate -d examples/dtd/XMLSchema.dtd examples/xsd/XMLSchema.xsd
Print file names¶
-
-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:
validate -x schema.xsd *.xml -l
-
-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:
validate -x schema.xsd *.xml -L | xargs rm
Footnotes
[1] | XML Schema 1.0 and 1.1 |
[2] | XML Document Type Definition |
[3] | RELAX NG Specification |