---
title: "XML and DTD; Teacher XML document"
subject: "Web Technology"
chapter: 7
key: "semester_7/web_technology:xml-teacher-dtd"
source: https://www.bctengineer.com/q/semester_7/web_technology/xml-teacher-dtd
site: BCT Engineering
---

# XML and DTD; Teacher XML document

Web Technology · Chapter 7: Information representation and sharing – XML

- Asked in: 2076 Chaitra, 2+3+5 marks
- Video: [L20: XML DTD – Document Type Definition | Internal, External DTD with example | Web Technology](https://www.youtube.com/watch?v=OjwV5X1Ugvs) — Easy Engineering Classes
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_7/web_technology/xml-teacher-dtd
- Whole chapter: https://www.bctengineer.com/md/semester_7/web_technology/chapter-7

## Question

Define XML? What is DTD (Document Type Definition)? Design well-formed XML document for "Teacher". Teacher should have data such as Name, Address, Phone, Age and Email. Name should contain FirstName and LastName.

## Short answer

XML is a markup language for structured data; DTD defines allowed elements and attributes. The provided XML document is well-formed with a root <teachers>, each <teacher> containing name (with firstname and lastname), address, phone, age, and email.

## Answer

#### XML
**XML (eXtensible Markup Language)** is a W3C standard, text-based markup language used to describe and store data in a structured, self-describing way. Unlike HTML, XML has no predefined tags: users define their own tags, and its purpose is to carry data, not display it. XML must be well-formed (one root element, properly nested and closed tags, quoted attributes, case-sensitive names). It is platform and software independent, so it is widely used for data exchange.

#### DTD (Document Type Definition)
A **DTD** defines the legal structure of an XML document: which elements and attributes are allowed, their order, number and content type. An XML document that is well-formed and follows its DTD is **valid**. A DTD can be internal (inside `<!DOCTYPE ...>`) or external (a `.dtd` file). Main declarations:
- `<!ELEMENT name (child1, child2)>` – element and its content model
- `<!ELEMENT name (#PCDATA)>` – text-only element
- `<!ATTLIST element attr CDATA #REQUIRED>` – attribute list
- Symbols: `,` sequence, `|` choice, `+` one or more, `*` zero or more, `?` optional.

#### Well-formed XML for "Teacher" with its DTD
```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE teachers [
  <!ELEMENT teachers (teacher+)>
  <!ELEMENT teacher (name, address, phone, age, email)>
  <!ATTLIST teacher id CDATA #REQUIRED>
  <!ELEMENT name (firstname, lastname)>
  <!ELEMENT firstname (#PCDATA)>
  <!ELEMENT lastname (#PCDATA)>
  <!ELEMENT address (#PCDATA)>
  <!ELEMENT phone (#PCDATA)>
  <!ELEMENT age (#PCDATA)>
  <!ELEMENT email (#PCDATA)>
]>
<teachers>
  <teacher id="T01">
    <name>
      <firstname>Ram</firstname>
      <lastname>Sharma</lastname>
    </name>
    <address>Lalitpur, Nepal</address>
    <phone>9841000000</phone>
    <age>35</age>
    <email>ram.sharma@example.com</email>
  </teacher>
  <teacher id="T02">
    <name>
      <firstname>Sita</firstname>
      <lastname>Karki</lastname>
    </name>
    <address>Kathmandu, Nepal</address>
    <phone>9851000000</phone>
    <age>29</age>
    <email>sita.karki@example.com</email>
  </teacher>
</teachers>
```

#### Why it is well-formed
- It has a single root element `<teachers>`.
- Every start tag has a matching end tag and elements are properly nested.
- Attribute values are quoted and element names are consistent in case.
- `name` contains `firstname` and `lastname`, as required.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What is the purpose of XML?
   A. To describe and store data in a structured self-describing way, not to display it
   B. To run scripts
   C. To style pages
   D. To display pages with fixed tags
   Answer: A. XML carries data using user-defined tags; HTML displays. (Key point: XML purpose)

2. Which is required for XML to be well-formed?
   A. No attributes allowed
   B. One root element, properly nested and closed tags, quoted attribute values
   C. A DTD file always
   D. Predefined tags only
   Answer: B. Well-formed means correct syntax; a DTD is needed only for validity. (Key point: Well-formed rules)

3. In (teacher+), what does + mean?
   A. Zero or more
   B. Zero or one
   C. One or more
   D. Exactly two
   Answer: C. + means one or more, * zero or more, ? optional. (Key point: DTD symbol +)

4. When is an XML document valid?
   A. It has no root
   B. It is merely well-formed
   C. It has an external file
   D. It is well-formed and follows its DTD
   Answer: D. Valid = well-formed + conforms to DTD. (Key point: Valid vs well-formed)

## Close variants set in other exams

- [XML Student document and its DTD](https://www.bctengineer.com/question.md?key=semester_7%2Fweb_technology%3Axml-student-dtd) — Web Technology, chapter 7, asked 2×

## Related in Web Technology

- [Well formed XML and DTD example](https://www.bctengineer.com/question.md?key=semester_7%2Fweb_technology%3Awell-formed-xml-dtd-example) — Web Technology, chapter 7
