xmllint

Suggested use

To validate XML data with a DTD stored in the same file:

xmllint --valid --noout DataFileWithDTD.xml

To validate XML data against a separately stored DTD:

xmllint --dtdvalid DTD.dtd --noout DataFileWithoutDTD.xml

To validate XML data against a separately stored XML Schema definition:

xmllint --schema XMLSchemaFile.xsd --noout DataFile.xml

Example

test.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="name" type="xs:string"/>
</xs:schema>

test.xml

<?xml version="1.0" encoding="UTF-8" ?>
<name>foo</name>

To Verify

xmllint --noout --schema test.xsd test.xml

bad.xml

<?xml version="1.0" encoding="UTF-8" ?>
<name>foo</name>
<number>123</number>

Better Checker

echo "Good test"
xmllint --noout --schema test.dtd test.xml
echo "Bad Test"
xmllint --noout --schema test.dtd bad.xml

Output

I see the following output

Good test
test.xml validates
Bad Test
bad.xml:3: parser error : Extra content at the end of the document
<number>123</number>

Which is correct.

Using an xsd

An xsd is an XML that contains a DTD definition (I believe).

xmllint --noout --schema test.xsd good.xml

XSD

note.xsd

<!DOCTYPE note
[
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>

The DTD above is interpreted like this:

  • !DOCTYPE note defines that the root element of the document is note
  • !ELEMENT note defines that the note element must contain the elements: "to, from, heading, body"
  • !ELEMENT to defines the to element to be of type "#PCDATA"
  • !ELEMENT from defines the from element to be of type "#PCDATA"
  • !ELEMENT heading defines the heading element to be of type "#PCDATA"
  • !ELEMENT body defines the body element to be of type "#PCDATA"

#PCDATA means parse-able text data !

note.xml

For the Above XSD this is a valid XML file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "Note.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

Checking XML and XSD

xmllint --schema note.xsd --noout note.xml