Validating An XML Document: Week 18 Tutorial 3
Validating An XML Document: Week 18 Tutorial 3
Document
Week 18
Tutorial 3
Creating a Valid Document
• Validation is required to ensure certain necessary elements are never omitted.
• For example, each customer order should include a customer name, address and phone
number.
• When the parser loads an XML file, it compares the DTD rule set with the content
(XML file) so that errors can be found easily.
• Using a standard DTD to verify the data received from the outside world.
Well formed and Valid XML
• An XML document is well-formed if all the tags are correctly formed and follow XML
rules.
• An XML document is valid if the XML documents succeeds validation against a
DTD.
• A valid XML refers to semantics whereas well-formed XML refers to syntax.
Declaring a DTD
• There can only be one DTD per XML document.
• While there can only be one DTD, it can be divided into two part: internal subset and
external subset.
•
Declaring a DTD (internal subset)
An internal subset is declarations placed in the same file as the document content.
<?xml version=“1.0”?>
<!DOCTYPE faculty[
<!ELEMENT faculty (title,code,lecturer,tutor)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT code (#PCDATA)>
<!ELEMENT lecturer (#PCDATA)>
<!ELEMENT tutor (#PCDATA)>
]>
<faculty>
<title>Emerging Programming and Technologies</title>
<code>CS5004NI</code>
<lecture>Pradhumna Dhungana</lecturer>
<tutor>Pradhumna Dhungana & Pratik Panta</tutor>
</faculty>
Declaring a DTD (external subset)
An external subset is located in a separate file.
<?xml version=“1.0”?>
<!DOCTYPE faculty SYSTEM “faculty.dtd”>
<faculty>
<title>Emerging Programming and Technologies</title>
<code>CS5004NI</code>
<lecture>Pradhumna Dhungana</lecturer>
<tutor>Pradhumna Dhungana & Pratik Panta</tutor>
</faculty>
• Faculty is a root.
• It is the name of the document’s root element.
• Declarations are the statements that comprise the DTD.
Declaring Document Elements
• Defines five different types of element content:
Any
Empty
#PCDATA
Elements
Mixed