XML Interview Questions

What is XML DTD? How to declare in XML file?

XML DTD: An XML document’s DTD, or document type definition, is a set of rules you specify. Why is this significant? Your savings account data could be misinterpreted as your checking account data, which could lead to all kinds of difficulty if the format of your XML document is inconsistent throughout. We can specify the elements of an XML document and position them as we see fit thanks to XML DTD. However, there’s still more! Additionally, entities and common components for XML documents can be defined using DTDs.

Here’s what XML DTDs do :

  • Define all elements and attributes o f an XML document.
  • Define the order in which elements and attributes can occur.
  • Define all entities that can be used in an XML document.
  • Define the document type.

XML DTD : Declaring DTD in XML file

You can keep XML DTDs in a separate file and store it with your XML document. The DTD must be at the top of your file (following the?xml version=”1.0″?> declaration) if it is to be contained within your XML file. An internal DTD’s start has the following format:

<!DOCTYPE mydoctype [
...
...
...
]

Mo re likely, yo u will store your DTD outside your XML document. The beginning o f an XML file that has an external DTD lo o ks like this:

<!DOCTYPE mydoctype SYSTEM
"http://www.mysite.com/dtds/mydoctype.dtd">

Let’s break down and explain this code:

The document type is mydoctype. Your XML document’s doctype may be financial or banking if it contains personal financial data. If your DTD is a different file from your XML content, SYSTEM is used. Your DTDs can be defined as either

PUBLIC or SYSTEM, based on the application. PUBLIC denotes that anybody may use the DTD. This can be true if your DTD defines the XML standard for banking transactions. Use SYSTEM if you are using your own DTD. (You won’t need to worry about this because the DTDs you use for this post will be internal.)

http://www.mysit e.co m/dtds/mydoctype.dtd indicates the location (o r URL) of the DTD file if your DTD is separate file. Here’s an example of an XML document with an internal DTD:

<?xml version="1.0"?>
<?xml-stylesheet href="http://courses.oreillyschool.com/introxml/xsl/PhoneBook.xsl" typ
e="text/xsl"?>
<!DOCTYPE PhoneBook [
<!ELEMENT PhoneBook (Listing+)>
<!ELEMENT Listing (First,Last,Phone+)>
<!ELEMENT First (#PCDATA)>
<!ELEMENT Last (#PCDATA)>
<!ELEMENT Phone (#PCDATA)>
<!ATTLIST Phone
Type CDATA #REQUIRED>
]>
<PhoneBook>
<Listing>
<First>Alex</First>
<Last>Chilton</Last>
<Phone>1-800-123-4567</Phone>
</Listing>
<Listing>
<First>Laura</First>
<Last>Chilton</Last>
<Phone>1-800-234-5678</Phone>
</Listing>
</PhoneBook>

Here’s an example o f an external DTD:

<!ELEMENT PhoneBook (Listing+)>
<!ELEMENT Listing (First,Last,Phone+)>
<!ELEMENT First (#PCDATA)>
<!ELEMENT Last (#PCDATA)>
<!ELEMENT Phone (#PCDATA)>
<!ATTLIST Phone
Type CDATA #REQUIRED>

External DTDs usually have a .dtd extension. Yo u can save them anywhere as long as yo u specify the pro per location of your DTD in your XML file.

We will specify an internal DTD for our phone boo k file. Elements of the DTD are the same as XML elements. An element declaration in XML lo o ks like this:

<!ELEMENT First (#PCDATA)>

What does this assertion actually mean? The element’s name, which will appear in the XML document, comes first. (#PCDATA) indicates that the First element’s content will be parsed character data, also known as PCDATA, which is text that will be processed by an XML parser.(CDATA, or character data, is text that an XML parser won’t be able to understand.)

You can specify as many elements as yo u need. So you may want to specify the order in which elements will be accessed as well. Using the phone book XML document, we may want to specify that the First name appears before the Last name. The DTD would look like this:

<!ELEMENT Listing (First,Last)>
<!ELEMENT First (#PCDATA)>
<!ELEMENT Last (#PCDATA)>

No w in our Listing tag, two elements must appear: First and Last (in that order).Now, modify your phoneBook.xml file as shown:


<?xml version="1.0"?>
<?xml-stylesheet href="http://courses.oreillyschool.com/introxml/xsl/PhoneBook.xsl" typ
e="text/xsl"?>
<!DOCTYPE PhoneBook> [
<!ELEMENT PhoneBook (Listing+)>
<!ELEMENT Listing (First,Last,Phone+)>
<!ELEMENT First (#PCDATA)>
<!ELEMENT Last (#PCDATA)>
<!ELEMENT Phone (#PCDATA)>
<!ATTLIST Phone
Type CDATA #REQUIRED>
]>
<PhoneBook>
<Listing>
<First>Alex</First>
<Last>Chilton</Last>
<Phone Type="cell">1-800-123-4567</Phone>
<Phone Type="home">1-800-123-4568</Phone>
<Phone Type="work">1-800-123-4569</Phone>
</Listing>
<Listing>
<First>Laura</First>
<Last>Chilton</Last>
<Phone>1-800-234-5678</Phone>
</Listing>
</PhoneBook>

Save & Validate

Error: Element Phone does not carry attribute Type on line 22

What do es this mean? Your XML document was compared to its internal DTD (the very same one we just created). The DTD specifies that the element Phone must have an attribute o f Type, always. A Phone element in our XML file doesn’t have a Type attribute, so our XML parser gave us an error. Go ahead and add the Type attribute and set it to “ho me.” Validate the document once again; that error will go away.

Now try removing the Phone elements completely. Take out the code in red as shown:

<?xml version="1.0"?>
<?xml-stylesheet href="http://courses.oreillyschool.com/introxml/xsl/PhoneBook.xsl" typ
e="text/xsl"?>
<!DOCTYPE PhoneBook [
<!ELEMENT PhoneBook (Listing+)>
<!ELEMENT Listing (First,Last,Phone+)>
<!ELEMENT First (#PCDATA)>
<!ELEMENT Last (#PCDATA)>
<!ELEMENT Phone (#PCDATA)>
<!ATTLIST Phone
Type CDATA #REQUIRED>
]>
<PhoneBook>
<Listing>
<First>Alex</First>
<Last>Chilton</Last>
<Phone Type="cell">1-800-123-4567</Phone>
<Phone Type="home">1-800-123-4568</Phone>
<Phone Type="work">1-800-123-4569</Phone>
</Listing>
<Listing>
<First>Laura</First>
<Last>Chilton</Last>
<Phone Type="home">1-800-234-5678</Phone>
</Listing>
</PhoneBook>

Save & Validate..You will get error as per below,

Error: Element Listing content does not follow the DTD, expecting (First , Last , Phone
+), got (First Last ) on line 12

The DTD demands that at least one Phone element come after the Last element, according to this mistake. An error is raised because there are none. To fix the mistake, restore the phone numbers that were erased.

Play around with this idea for a bit. Modify your XML file and DTD to purposefully violate validation. not the ones that are produced. More information on the syntax of DTDs is provided below. Try each one!