CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

XML – XML Structure and Creating DTD

Before you read this article, I recommend reading our previous Article “XML: An Introduction” where we have discussed XML. XML files also look like, HTML pages. Web browsers translate these HTML pages to render the content provided in the HTML.

In this article, we take a look at XML structure, what is DTD and how to create a DTD.

XML files are completely filled with tags. So the question is which tags do we need to use and who will confirm the tags?

For example, when we write java programs; those programs are compiled and executed by some special programs; can understand that code. In the same way, to enable us to understand the XML files we need to provide some set of rules through special files called DTD (Document Type Definition) files. Apparently, we know that DTD’s are defined the structure of XML like what are the names of tags and attributes, etc.

Now, we can say that every XML file behind a DTD is definitely to enable understanding the XML file. XML doesn’t have any predefined set of tags. The tags which we write in DTD are the ultimate tags that can be allowed in XML.

Rules we can follow to define a DTD:

  • We can first declare what is the encoding format of XML
  • Which are the root element and their attributes.
  • How many child elements are available with the root element.
  • How many times each child element can appear
  • For every element, the attribute is mandatory or optional
  • Every child element is required to declare in the order as they are defined in the DTD or not.

Well-formed and valid XML:

At the time of writing XML, we are frequently heard about the well-formed (adheres to the syntax rules) and validity of XML.

The first step, we need to do after writing the XML is, checking for syntax rules. A well-formed validation check is for whether this XML is syntactically following the rules or not.

Syntactical rules of XML are nothing but:

  1. Every XML tag should be closed properly at the same level as they are opened.
  2. No-tag should start with special characters. Every tag must start with the alphabet only.
  3. Tags must be case-sensitive.
  4. Every XML file must contain only one root element.
  5. All attributes must be enclosed within double quotes(“ “)

Once an XML file is well-formed then only we are allowed to check whether it is valid or not.

XML file validity completely depends upon the DTD rules of that XML.

DTD controls the XML only structural manner but not the content manner i.e XML allows any data within the tags but the tag names and their sequence follows as it is declared in the DTD.

Now, we can continue our discussion with how to write a DTD

  • We can write DTD and XML as in a single file or two different files.
  • If we write DTD and XML in two different files, now as a part of XML we need to specify the location of DTD and if the DTD is located in the remote machine we can mention the URL also.

Syntax:

<!DOCTYPE root-element-name  SYSTEM "location" >

Example:

<!DOCTYPE student SYSTEM "d:/student.dtd" >
  • In DTD we are declaring elements those elements names are becoming as tags in XML.
  • In DTD elements are 2 types. Simple element and compound element.
  • The simple element can be declared as follows:

Syntax:

<!ELEMENT elename (content model)>

Example:

<!ELEMENT sno (#PCDATA)>
  • In DTD we are allowed to declare #PCDATA only as a content model.
  • Compound elements mean elements within another child element. The compound element contains a list of children with, comma (“,”) separation.

Syntax:

<!ELEMENT elename (child1,child2,child3l)>

Example:

<!ELEMENT student (sno,sname)>

Attributes can be declared as follows:

Syntax:

<!ATTLIST elementname attributename type value>

Example:

<!ATTLIST book author CDATA #REQUIRED>

Description of the above line is, author, is the attribute for an element “book” and it is not optional means it must be declared in XML. Remember that by default attributes are optional.

Below is the sample program for DTD:-

<?xml version="1.0" encoding="UTF-8"?>

<!ELEMENT student (sno,sname)>

<!ELEMENT sno (#PCDATA)>

<!ELEMENT sname (#PCDATA)>

According to above, DTD “student” is a root element and it is a compound element so it contains child elements named sno, sname.

an equivalent XML file for above DTD is below:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE order SYSTEM "D:\XMLapps\order.dtd">
<student>
<sno>15</sno>
<sname>jan</sname>
</student>

So far we are have discussed how to create DTD.

Now we will take a simple use-case with certain conditions; based on these, we will create a DTD (Document Type Definition) and an XML.

Step 1. Let’s look at a simple use-case; a billing generating system with the below rules:

  1. The Document must start with the element “bill”.
  2. The “bill” element should contain, 3 child elements named as “customer address”, “package” and an “amount”. These 3 child elements should appear in the same order as they are defined in the DTD.
  3. “customer address” element contains again 6 child elements named as “name”, “mobile”, “street”, “city”, “country” and “hno”. But there is no condition in the order of appearing these elements under “customer address”.
  4. “package” should contain 2 child elements named “speed” and “type”. The condition is, these two child elements should appear only one time in the same order as they defined in the DTD.
  5. “amount” should contain only one child element named “rupees” and it should appear only one time.

See the below DTD it satisfies all the 5 conditions we mentioned above.

Look at the syntax of “customerAddress”; we mentioned that the children of this element should appear in any order so that we are using the | (OR) operator with the “+” symbol.

As we discussed above, for all elements we need to specify the content model as #PCDATA only.

Save the following DTD as “D:\XMLapps\bill.dtd” because we need to refer it in XML document.

bill.dtd:

<?xml version="1.0" encoding="UTF-8"?>

<!ELEMENT bill (customerAddress,package,amount)>
<!ELEMENT customerAddress (name|mobile|hno|street|city|country)+>
<!ELEMENT name (#PCDATA)>
<!ELEMENT mobile (#PCDATA)>
<!ELEMENT hno (#PCDATA)>
<!ELEMENT street (#PCDATA)>
<!ELEMENT city (#PCDATA)>
<!ELEMENT country (#PCDATA)>

<!ELEMENT package (speed,type)>
<!ELEMENT speed (#PCDATA)>
<!ELEMENT type (#PCDATA)>

<!ELEMENT amount (rupees)>
<!ELEMENT rupees (#PCDATA)>

The explanation for the above DTD:

  • Every DTD starts with rooting an element. In our use case bill is our root element. Generally, any root element is not a simple element. so we also took that bill as the compound element with 3 child elements.
  • after declaring the root element we need to elaborate the all child elements separately.
  • if the child element is a simple element we can directly declare them. In our use case, no element is a simple element inside the root element.
  • we continue this process until we found the simple element.
  • In our use case package is one compound element look at how we declare that element. We first declare the compound element with its child, after we declare the child’s individual with its content model.
<!ELEMENT package (speed,type)>
<!ELEMENT speed (#PCDATA)>
<!ELEMENT type (#PCDATA)>

By following the above DTD we need to write the XML now.
The first step to writing XML document is to specify the DTD location of the current XML
If you want to test it remove the order of the elements and again check the good firmness and validity then you can get to know it is satisfying the above DTD or not.

bill.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE bill SYSTEM "D:\XMLapps\bill.dtd">
<bill>
 <customerAddress>
    <name>murthy</name>
    <mobile>9848022338</mobile>
    <street>gandhi nagar</street>
    <city>hyderabad</city>
    <country>India</country>
    <hno>3-17</hno>
 </customerAddress>
 <package>
    <speed>4mbps</speed>
    <type>wire</type>
 </package>
 <amount>
    <rupees>1500</rupees>
 </amount>
</bill>

We will discuss what are the drawbacks of DTD, what should we do to overcome those drawbacks in the upcoming Articles.

Thank You.

XML – XML Structure and Creating DTD

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top