Updated April 3, 2023
Definition of XML DTD
Document Type Definition (DTD) defines the schema of an XML document which includes elements, attributes in it. If the XML documents are conformed to the DTD format then it is valid and it is used in business-to-business applications where XML documents are exchanged in which they are defined using extended Backus-Naur form. Multiple documents and different applications share DTDS also defines the order of elements.DTD are defined in the Document with the declaration <!DOCTYPE > and each XML document holds one DTD.
How DTD Works in XML?
DTD is also the schema language preferred in mark up language. The general Syntax is given below:
<!DOCTYPE root element name
[
declaration1
declaration2
<!ELEMENT> <ATTLIST> <!ENTITY> <!NOTATION>
]>
In the above syntax, the DTD name is the root element name and followed by options which say about the schemas and types. The keyword! DOCTYPE should be uppercase. Let’s see Element declarations.
The working of DTD is performed by the following steps:
- First, create a DTD file for the respective XML Document.
- Next outline the structure of the document.
- Initiate with the root node which is the same as DOCTYPE. You can create DTD either internal or external references.
- Include all the elements, attributes, entities for the file. The parser eliminates empty elements.
Element Declarations
The element specifications with the sequence of its elements are stated as
<!ELEMENT name (seq of elements)>
Ex: <!ELEMENT ship (crew name, location, date)> // this statement is often termed as generic identifier.
Also, the element specifies the number of occurrences of the child elements using (+, *,? ).
- + sign indicates two or more frequency
- *indicates the element can appear more number of times.
- ? Indicates elements can occur only once.
The following declaration is
<!ELEMENT pizza (onion? Cheese*((veg|noveg) + |topping))>
The above statement implies that the pizza element can have one onion elements followed by one or more cheese and so on.
The respective XML file could be
<pizza>
<onion> fried </onion>
<cheese> thin </cheese>
<cheese> thick </cheese>
<topping>oregano </topping>
</pizza>
1. Attribute Declarations
The attributes for a given element is designed by the following rule:
<!ATTLIST element name attribute names 1 attribute type restriction/Default >
Ex:
here attribute is specified using the keyword ATTLIST, the element name is included for the respective attributes unless they are optional. The attribute types include PCDATA, tokens, entity, notation. Last is restriction/default they are placed based on the occurrences of the values. The attribute default includes #IMPLIED, #REQUIRED, #FIXED. The implied specifies the attribute value doesn’t appear and required implies the attribute value is present and fixed denotes a constant value.
2. Defining Entities
Entity is used to specify special characters. The entity declaration is
<!ENTITY entity_name SYSTEM "URL path">
Ex:
<!ENTITY SYSTEM "http://www. ckjd.com/pot.dtd">
As DTD is model of the XML document it talks about the elements, attributes being used which are essential and optional as they are easy to validate the document and there are two types of DTDs namely,
- Internal DTD
- External DTD
3. Internal DTD
This type of DTD is declared inside the XML Document. It is declared as.
<!DOCTYPE root name [DTD related specification]>
Ex: <!DOCTYPE healthcare [
<!ELEMENT healthcare (#PCDATA)>
]>
The content inside the square brackets is considered to be the internal subset. And the keyword! ELEMENT is element declarations, PCDATA is the parsed character data which are parsed by the XML parsers.
4. External DTD
This type of DTD is declared outside the XML file with a separate file. External DTD is used in multiple XML documents, the updation done in this file affects all the XML document which is quite easy while changing the input file. In external DTD the ‘standalone’ keyword is set to “no”. The external content is specified using a keyword ‘PUBLIC’ and ‘SYSTEM’. The public keyword is used outside the XML document followed by a URL (specifies the path).
Note: Multiple DTDs are allowed in which both external and internal DTDs are combined.
<!DOCTYPE root-name SYSTEM " XML file-name">
There are two types of External DTD: Private and public.
Examples to Implement DTD in XML
Following are the examples of dtd in xml are given below:
Example #1 – External DTD
Here the DTD file is created external and saved as stck.dtd and the corresponding element name is declared in the separate XML file.
stck.dtd
<?xml version="1.0"?>
<!ELEMENT Stockmarket (sname,branch,contact)>
<!ELEMENT sname (#PCDATA)>
<!ELEMENT branch (#PCDATA)>
<!ELEMENT contact (#PCDATA)>
XML file
vision.xml
<?xml version = "1.0" encoding = "UTF-8" standalone = "no" ?>
<!DOCTYPE Stockmarket SYSTEM "stck.dtd">
<Stockmarket>
<sname>Bluechip tech</sname>
<branch>nine</branch>
<contact>(022) 245-8597</contact>
</Stockmarket>
Output:
Example #2 – Using Entities
actor.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE Actor [
<!ELEMENT Actor ((aname)+, hollywood)>
<!ELEMENT aname (fname, lname)>
<!ELEMENT fname (#PCDATA)>
<!ELEMENT lname (#PCDATA)>
<!ELEMENT hollywood (#PCDATA)>
<!ENTITY UofT SYSTEM "fil.xml">
]>
<Actor>
<aname>
<fname>Markdev</fname>
<lname>carylon</lname>
</aname>
&UofT;
</Actor>
fil.xml
<holywood>
Mark of the film industry
</holywood>
Output:
Example #3
Following is an XML file with DTD declared inside the XML file-Internal DTD which is embedded inside the keyword DOCTYPE. In the below example the element node university has three fields and those are declared of the type PCDATA.
auto.xml
<?XML version="1.0" standalone="yes"?>
<!DOCTYPE University [
<!ELEMENT University (collegename, type, email)>
<!ATTLIST University
id CDATA #REQUIRED>
<!ELEMENT collegename (#PCDATA)>
<!ELEMENT type (#PCDATA)>
<!ATTLIST University
department CDATA #IMPLIED>
<!ELEMENT email (#PCDATA)>
]>
<University id="02">
<collegename>Harvard University</collegename>
<type department="Science and Humanities" >Head of the Department</type>
<email>[email protected]</email>
</University >
Output:
Example #4 – Internal DTD
simp.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE articles [
<!ELEMENT articles (Movie+) >
<!ELEMENT article ( name, month, author, (reviews | feedback)+)>
<!ATTLIST article id ID #REQUIRED>
<!ELEMENT name (#PCDATA)>
<!ELEMENT month (#PCDATA) >
<!ELEMENT author (#PCDATA)>
<!ATTLIST author name CDATA #IMPLIED>
<!ELEMENT reviews (#PCDATA)>
<!ELEMENT feedback (#PCDATA)>
<!ATTLIST reviews lang CDATA #IMPLIED>
]>
<articles>
<article id="x34675">
<name>Apache Spark Architecture</name>
<month>december</month>
<author name="kay vennisla"/>
<reviews lang=""/>
<feedback > high rating</feedback>
<reviews lang="de">The best content with diagrams</reviews>
<!-- most value content -->
</article>
</articles>
Output:
Validation Checker
There are many tools to validate the XML document against DTD
Conclusion
Therefore, we have seen how DTD works in the XML. The standard DTD were used by many applications to verify the valid data received from the external sources before it is sent to the other clients. Therefore, it is a key ingredient of the DTD to examine/test the xml file before it is given to the business process.
Recommended Articles
This is a guide to XML DTD. Here we also discuss the definition and how dtd works in xml? along with different examples and its code implementation. You may also have a look at the following articles to learn more –