To ensure validity in your XML file, a document type definition (DTD) is used to provide a guideline for describing the syntax and grammatical structure of the file's content. It allows you to create your own markup language: new elements and attributes can be defined from scratch that best fit the information you want to encapsulate. The process of formally defining a language in XML is called document modeling. The DTD is one form of document modeling. It makes it possible to determine whether or not an XML document conforms to the document type. We'll use it to validate DIRSIG's XML input files.
A DTD can be declared inline in your XML document, or it can be referenced externally. We will use the external reference approach. The following XML example illustrates the content of DIRSIG's platform motion description, in which the DTD is externally referenced at the start of the description via the <DOCTYPE> tag. Our content is expected to conform to the DTD pointed to by the SYSTEM's uniform resource identifier.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE platformmotion SYSTEM "http://www.cis.rit.edu/~pxlpci/dtd/platformmotion.dtd">
<platformmotion type="generic" >
<method type="raw" />
<uncertainty enabled="0" >
<xlocation type="normal" >
<variance>0.00</variance>
</xlocation>
<ylocation type="normal" >
<variance>0.00</variance>
</ylocation>
<zlocation type="normal" >
<variance>0.00</variance>
</zlocation>
</uncertainty>
<data rotationorder="zyx" spatialunits="meters" angletype="absolute" angularunits="radians" >
<entry>
<datetime type="relative" >0.00</datetime>
<xlocation>243.84</xlocation>
<ylocation>243.84</ylocation>
<zlocation>304.80</zlocation>
<xrotation>0.00</xrotation>
<yrotation>0.00</yrotation>
<zrotation>0.00</zrotation>
</entry>
</data>
</platformmotion>
The DTD associated with the aforementioned platform motion description would look like this:
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT platformmotion (method, uncertainty, data)>
<!ATTLIST platformmotion type (generic) #REQUIRED>
<!ELEMENT method EMPTY>
<!ATTLIST method type (raw) #REQUIRED>
<!ELEMENT uncertainty (xlocation, ylocation, zlocation)>
<!ATTLIST uncertainty enabled (0|1) #REQUIRED>
<!ELEMENT xlocation (#PCDATA|variance)*>
<!ATTLIST xlocation type CDATA #IMPLIED>
<!ELEMENT variance (#PCDATA)>
<!ELEMENT ylocation (#PCDATA|variance)*>
<!ATTLIST ylocation type CDATA #IMPLIED>
<!ELEMENT zlocation (#PCDATA|variance)*>
<!ATTLIST zlocation type CDATA #IMPLIED>
<!ELEMENT data (entry+)>
<!ATTLIST data rotationorder CDATA #REQUIRED
spatialunits (meters) #REQUIRED
angletype (absolute) #REQUIRED
angularunits (radians) #REQUIRED>
<!ELEMENT entry (datetime, xlocation, ylocation, zlocation,
xrotation, yrotation, zrotation)>
<!ELEMENT datetime (#PCDATA)>
<!ATTLIST datetime type (absolute|relative) #REQUIRED>
<!ELEMENT xrotation (#PCDATA)>
<!ELEMENT yrotation (#PCDATA)>
<!ELEMENT zrotation (#PCDATA)>
The free tool xmllint, found in most UNIX-like operating systems such as Linux and Mac OS X, is very handy for checking an XML file for errors. It reports very descriptive error messages (mismatched start/end tags, missing characters, etc.) and points out where the errors are. It can also be used to indent and "pretty print" a well-formed XML document.
For example, to validate an XML file named dirsig.xml, open up a terminal window and change into the directory where the file resides. Then type the following command after the shell prompt and press the return key:
prompt> xmllint --valid --noout dirsig.xml
If the file doesn't conform to the DTD, you will receive subsequent error messages along with the file line number where the error occurred. Otherwise, you should see no output.
There are also numerous websites which offer free XML validation checks, such as: http://www.xmlvalidation.com.