Tuesday, October 9, 2012

Validate XML and DTD

Back to school again, I found that I needed to write some XML and DTD by hand.

The most important aspect of the process is to have a valid XML document as well as a valid DTD. There are perhaps some online tools for this, but none of them are very intuitive.

However, there is a tool named xmllint which does exactly this.

It is included with libxml (package), so there's a great chance you already have it.

Here is an example on how to use it:


<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT editors (editor)>
<!ELEMENT editor  (title, desc?)>
<!ELEMENT title   (#PCDATA)>
<!ELEMENT desc    (#PCDATA)>


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE editors SYSTEM "editors.dtd">
    <!--Powerful text editor-->

And to validate the DTD and XML, execute the following from your command line:
xmllint --valid editors.xml  --dtdvalid editors.dtd

Note: use can use xmllint to validate XML Schemas just use the above command and replace --dtdvalid with with -schema and provide a schema file instead of the DTD file.

Hope that helps!

No comments:

Post a Comment