0% found this document useful (0 votes)
53 views

Validating An XML Document: Week 18 Tutorial 3

An XML document can be validated using a DTD (Document Type Definition) to ensure it contains required elements and follows specified structure. A DTD defines elements, attributes, and content for an XML document. It is declared internally within the XML code or externally in a separate file. Elements are declared using tags that specify allowed content such as text only, child elements only, or a mix of text and elements. Occurrence indicators like ?, +, * define how many times an element can appear.

Uploaded by

Darshan Stha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Validating An XML Document: Week 18 Tutorial 3

An XML document can be validated using a DTD (Document Type Definition) to ensure it contains required elements and follows specified structure. A DTD defines elements, attributes, and content for an XML document. It is declared internally within the XML code or externally in a separate file. Elements are declared using tags that specify allowed content such as text only, child elements only, or a mix of text and elements. Occurrence indicators like ?, +, * define how many times an element can appear.

Uploaded by

Darshan Stha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Validating an XML

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.

• However, some elements and attributes may be optional such as email.

• An XML document can be validated using DTD.

• DTD stands for Document Type Definition.


Document Type Definition
• It defines the structure of the documents.

• Defines elements and attributes of an XML document.

• Ensures all required elements are present in the document.

• Prevents undefined elements from being used.

• Specify what sort of content can appear and where.

• Define default values for attributes.


Why use DTD?
• For example, if you are giving XML document to other(third parties), DTD helps make
XML document in a format.

• 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.

• DTD define the content and structure of the document.

• For example, element, attributes, entities, notations, processing instructions,


comments, parameter entity references
Declaring a DTD(contd.)
• You create a DTD by first entering a document type declaration into your 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>

The file faculty.dtd:


<!ELEMENT faculty (title,code,lecturer,tutor)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT code (#PCDATA)>
<!ELEMENT lecturer (#PCDATA)>
<!ELEMENT tutor (#PCDATA)>
Declaring a DTD (internal subset)
• <!DOCTYPE faculty[
• <!ELEMENT faculty (title,code,lecturer,tutor)>
• <!ELEMENT title (#PCDATA)> Declaration

• <!ELEMENT code (#PCDATA)>


• <!ELEMENT lecturer (#PCDATA)>
• <!ELEMENT tutor (#PCDATA)>
• ]>

• 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

• Any : Can store any type of content.

• Empty: Elements store no content.

• Parsed Character Data : Can only contain parsed character data.

• Element: Can contain only child elements.


Declaring Document Elements
• Mixed: Can contain both text string and child elements.

Examples: <!ELEMENT elementname ANY>

<!ELEMENT elementname empty> (<!ELEMENT br EMPTY>)

<!ELEMENT elementname (#PCDATA)

<!ELEMENT elementname (child1, child2)>

<!ELEMENT elementname (#PCDATA|childelement )


Modifying Symbols
• Indicates the number of occurrences of each element.

✔ question mark (?), allow zero or one

✔ plus sign (+), allow one or more


✔ an asterisk (*), allow zero or more.
• For Example, <!ELEMENT faculty (title,code,lecture,tutor+)>

You might also like