Validating XML document with a DTD


 

Validating XML document with a DTD

If an xml document is well formed i.e. syntactically correct, then it doesn't mean it is valid also. To consider an XML document valid it must be validated, or verified, against a DTD.

If an xml document is well formed i.e. syntactically correct, then it doesn't mean it is valid also. To consider an XML document valid it must be validated, or verified, against a DTD.

Validating XML document with a DTD

If an xml document is well formed i.e. syntactically correct, then it doesn't mean it is valid also. To consider an XML document valid it must be validated, or verified, against a DTD.

DTD defines the required elements, optional elements, number and order of elements, attributes for elements etc. So your xml file must follow the rules defined in dtd. If your xml file defines the dtd name to ensure the validity of the file and it conforms the rules defined in dtd then the xml file is considered as valid xml document.

Validation of xml is optional. If you require the xml file to follow some rules defined in dtd then you can reference a dtd from xml document and check validation using xml parser during processing.

Lets take an example:

articles.dtd

<!ELEMENT articles (article)+>
<!ELEMENT article (title,url,img)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT url (#PCDATA)>
<!ELEMENT img (#PCDATA)>
<!ATTLIST articles type CDATA #REQUIRED>

articlesdata.xml

<?xml version="1.0" standalone="no"?>
<!DOCTYPE articles SYSTEM "articles.dtd">
<articles>
  <article>
    <title>What is XML?</title>
    <url>http://roseindia.net/tutorial/xml/what-is-xml.html</url>
    <img>http://roseindia.net/tutorialfiles/27000.invoicexml.gif</img>
  </article>
  <article>
    <title>Hello World XML</title>
    <url>http://roseindia.net/tutorial/xml/helloworldxml.html</url>
    <img>http://roseindia.net/tutorialfiles/27312.xml1.gif</img>
  </article>
</articles>

Validating With the XML Parser With Internet Explorer:

test.html

<html>
<body>
<script type="text/javascript">
var xmlFile = new ActiveXObject("Microsoft.XMLDOM");
xmlFile.async="false";
xmlFile.validateOnParse="true";
xmlFile.load("articlesdata.xml");

document.write("Error Reason: "+xmlFile.parseError.reason+"<br>");
document.write("Error Line: "+xmlFile.parseError.line);
</script>
</body>
</html>

The articlesdata.xml does not contain type attribute in articles tag so this is not valid xml document. When you run the above test.html in Internet Explorer then it prints output as below indicating the error reason and line of error.

Make change in articlesdata.xml file and add type attribute in articles element.

articlesdata.xml

<?xml version="1.0" standalone="no"?>
<!DOCTYPE articles SYSTEM "articles.dtd">
<articles type="technical">
  <article>
    <title>What is XML?</title>
    <url>http://roseindia.net/tutorial/xml/what-is-xml.html</url>
    <img>http://roseindia.net/tutorialfiles/27000.invoicexml.gif</img>
  </article>
  <article>
    <title>Hello World XML</title>
    <url>http://roseindia.net/tutorial/xml/helloworldxml.html</url>
    <img>http://roseindia.net/tutorialfiles/27312.xml1.gif</img>
  </article>
</articles>

Now the above xml is considered as Valid xml document. Run again the xml file in Internet Explorer. Now it gives output as below:

Ads