---
title: "Well formed XML and DTD example"
subject: "Web Technology"
chapter: 7
key: "semester_7/web_technology:well-formed-xml-dtd-example"
source: https://www.bctengineer.com/q/semester_7/web_technology/well-formed-xml-dtd-example
site: BCT Engineering
---

# Well formed XML and DTD example

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

- Asked in: year not given, 6 marks
- Video: [XML - hierarchical Model,  Well Formed Vs Valid, DTD](https://www.youtube.com/watch?v=y5vmMce6pU0) — StudyYaar.com
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_7/web_technology/well-formed-xml-dtd-example
- Whole chapter: https://www.bctengineer.com/md/semester_7/web_technology/chapter-7

## Question

What do you mean by well formed XML document? Explain DTD with example.

## Short answer

A well-formed XML document follows syntax rules: one root element, proper nesting, matching tags, quoted attributes, and escaped special characters. DTD defines allowed element structure and attributes. Example: <!DOCTYPE book [ <!ELEMENT book (title, author+, price)> with required CDATA attribute isbn, and title, author, price as #PCDATA.

## Answer

#### Well-formed XML document
A **well-formed** XML document follows the basic syntax rules of XML, so any XML parser can read it. Rules:
1. There must be exactly one **root element**.
2. Every start tag must have a matching end tag (or be self-closed, `<br/>`).
3. Elements must be **properly nested** (`<a><b></b></a>`, not `<a><b></a></b>`).
4. Tag names are **case-sensitive** (`<Name>` is not `</name>`).
5. Attribute values must be **quoted**, and an attribute cannot appear twice in an element.
6. Special characters must be escaped (`&lt;`, `&gt;`, `&amp;`, `&quot;`, `&apos;`).
7. Names must start with a letter or underscore and contain no spaces.

Well-formed does not mean valid; **valid** means it also follows a DTD or schema.

#### DTD (Document Type Definition)
A **DTD** defines the allowed structure of an XML document: element names, their order and nesting, number of occurrences, and attributes. It can be internal (in the document) or external (a separate `.dtd` file).

Declarations: `<!ELEMENT ...>`, `<!ATTLIST ...>`, `<!ENTITY ...>`. Content symbols: `,` sequence, `|` choice, `+` one or more, `*` zero or more, `?` optional, `#PCDATA` text.

#### Example
```xml
<?xml version="1.0"?>
<!DOCTYPE book [
  <!ELEMENT book (title, author+, price)>
  <!ATTLIST book isbn CDATA #REQUIRED>
  <!ELEMENT title  (#PCDATA)>
  <!ELEMENT author (#PCDATA)>
  <!ELEMENT price  (#PCDATA)>
]>
<book isbn="978-0-13-1">
  <title>Web Technology</title>
  <author>A. Sharma</author>
  <author>B. Rai</author>
  <price>650</price>
</book>
```
Here `book` must contain one `title`, one or more `author` elements and one `price`, in that order, and must carry the required attribute `isbn`. Anything else makes the document invalid.

## Check yourself

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

1. What is the difference between well-formed and valid XML?
   A. They mean the same thing
   B. Valid means correct syntax only; well-formed also needs a DTD
   C. Well-formed follows XML syntax rules; valid also conforms to a DTD or schema
   D. Well-formed documents must have a DTD; valid ones need not
   Answer: C. Well-formed is about basic syntax; valid additionally follows a DTD or schema. (Key point: Well-formed vs valid)

2. Which document is NOT well-formed?
   A. <a><b></b></a>
   B. <a><b></a></b>
   C. <a><br/></a>
   D. <a x="1"></a>
   Answer: B. Elements must be properly nested; <a><b></a></b> closes a before b. (Key point: Nesting and root rules)

3. In a DTD, what does author+ mean?
   A. Zero or more author elements
   B. An optional author element
   C. One or more author elements
   D. Exactly two author elements
   Answer: C. + means one or more occurrences, * means zero or more, ? means optional. (Key point: Content symbols in DTD)

4. Given <!ELEMENT book (title, author+, price)> and <!ATTLIST book isbn CDATA #REQUIRED>, which book is invalid?
   A. One with title, two authors, price and isbn attribute
   B. One with title, one author, price and isbn attribute
   C. One with title, price, author in that order with an isbn attribute
   D. One with title, three authors, price and isbn attribute
   Answer: C. The comma means a sequence, so the order must be title, author(s), price. (Key point: DTD example interpretation)

## Related in Web Technology

- [XML and DTD; Teacher XML document](https://www.bctengineer.com/question.md?key=semester_7%2Fweb_technology%3Axml-teacher-dtd) — Web Technology, chapter 7, asked 2×
- [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×
- [XML document, XSLT and WSDL](https://www.bctengineer.com/question.md?key=semester_7%2Fweb_technology%3Axml-xslt-wsdl) — Web Technology, chapter 7
