Validating XML Document using XML DTD
XML
with correct syntax is "Well Formed" XML. XML validated against a DTD
"Valid" XML.
What is "Well Formed" XML documents?
What is "Valid" XML documents?
What is "Well
Formed" XML documents?
- A "Well Formed" XML document has correct XML syntax.
- XML documents must have a root element
- XML elements must have a closing tag
- XML tags are case sensitive
- XML elements must be properly nested
- XML attribute values must be quoted
<?xml
version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Raju</to>
<from>Ravi</from>
<heading>Reminder</heading>
<body>Don't forget me
this weekend!</body>
</note>
What is "Valid" XML documents?
A
"Valid" XML document is a "Well Formed" XML document, which
also conforms to the rules of a Document Type Definition (DTD).
Example:
<?xml
version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE
note SYSTEM "Note.dtd">
<note>
<to>Raju</to>
<from>Ravi</from>
<heading>Reminder</heading>
<body>Don't forget me
this weekend!</body>
</note>
The
DOCTYPE declaration in the example above, is a reference to an external DTD
file.
XML DTD
<!DOCTYPE note [
<!ELEMENT note
(to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from
(#PCDATA)>
<!ELEMENT heading
(#PCDATA)>
<!ELEMENT body
(#PCDATA)> ]>
The
purpose of a DTD (Document Type Definition) is to define the legal building
blocks of an XML document.
It
defines the document structure with a list of legal elements and attributes.
It
can be declared inline inside an XML document, or as an external reference.
- Note: PCDATA is parsed character data
Why Use a DTD?
DTD
provides the following advantages:
- With a DTD, each of your XML files can carry a description of its own format
- With a DTD, independent groups of people can agree to use a standard DTD for interchanging data.
- Your application can use a standard DTD to verify that the data you receive from the outside world is valid.
- You can also use a DTD to verify your own data.