Skip to main content

XML and DTD; Teacher XML document

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.

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.

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 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.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Web Technology chapter 7 questions →