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
<!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.
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
<onion> fried </onion>
<cheese> thin </cheese>
<cheese> thick </cheese>
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 >
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">
<!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.
<!ELEMENT Stockmarket (sname,branch,contact)>
<!ELEMENT sname (#PCDATA)>
<!ELEMENT branch (#PCDATA)>
<!ELEMENT contact (#PCDATA)>
<?xml version = "1.0" encoding = "UTF-8" standalone = "no" ?>
<!DOCTYPE Stockmarket SYSTEM "stck.dtd">
Example #2 – Using Entities
<?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">
Mark of the film industry
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.
<?XML version="1.0" standalone="yes"?>
<!DOCTYPE University [
<!ELEMENT University (collegename, type, email)>
id CDATA #REQUIRED>
<!ELEMENT collegename (#PCDATA)>
<!ELEMENT type (#PCDATA)>
department CDATA #IMPLIED>
<!ELEMENT email (#PCDATA)>
<type department="Science and Humanities" >Head of the Department</type>
Example #4 – Internal DTD
<?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>
<name>Apache Spark Architecture</name>
<author name="kay vennisla"/>
<feedback > high rating</feedback>
<reviews lang="de">The best content with diagrams</reviews>
<!-- most value content -->
There are many tools to validate the XML document against DTD
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.
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 –