DTD Elements and Attributes

Estimated study time: 16 minutes.

A DTD defines both the elements a document can use and the attributes those elements are allowed to carry. Each has its own declaration syntax.

Declaring Elements

<!ELEMENT course (title, duration, modules)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT duration (#PCDATA)>
<!ELEMENT modules (module+)>
<!ELEMENT module (#PCDATA)>

Common content model symbols:

  • #PCDATA — parsed character (text) data
  • , — elements must appear in this exact order
  • + — one or more occurrences
  • * — zero or more occurrences
  • ? — optional, zero or one occurrence

Declaring Attributes

<!ATTLIST course
  id CDATA #REQUIRED
  level (beginner|intermediate|advanced) "beginner">

This says the course element must have an id attribute (required, free text), and may have a level attribute restricted to one of three values, defaulting to "beginner" if omitted.

Common Attribute Defaults

  • #REQUIRED — the attribute must always be present.
  • #IMPLIED — the attribute is optional.
  • #FIXED "value" — the attribute always has this exact value.
  • "default" — a specific default value used when the attribute is omitted.
💡 Tip: Use enumerated attribute values (like the level example above) whenever a field should only ever hold a fixed set of options — it catches typos at validation time instead of downstream.

Ready to go beyond the basics?

Get hands-on training, live mentorship, and placement support with the Data Analytics Course at Uncodemy.

Explore XML Course →