Coding/Learn/xml/xml dtd validation
XML & Schemas Foundational Course
01.Introduction & Tree Structure
02.Syntax & Well-Formedness Rules
03.Elements vs. Attributes & Best Practices
04.Namespaces (xmlns) & Prefix Resolution
05.CDATA Sections & XML Comments
06.DTD (Document Type Definition) & Entities
07.Schema Definition (XSD) & Simple Elements
08.XSD Complex Types & Order Indicators (sequence, choice, all)
09.XSD Restrictions, Facets & Pattern Matching (Regex)
10.DOM Architecture & Node Types
11.Accessing & Traversing XML DOM Nodes
12.Modifying, Creating & Deleting XML Nodes
13.DOMParser, XMLSerializer & Parsing Errors
14.AJAX & XMLHttpRequest (responseXML)
15.XPath Syntax, Predicates & Axes
16.XSLT Transformations & Templates (<xsl:template>)
17.XSLT Logic, Loops & Sorting (for-each, if, choose)
18.XQuery & FLWOR Expressions (For, Let, Where, Order, Return)
19.in Web Services: SOAP, WSDL, RSS & SVG
20.Capstone: Enterprise XML Data Pipeline & Validator
Course Certificate0%

Complete all lessons to earn

LearnDTD (Document Type Definition) & Entities
6 / 20 0%
Lesson 06 of 20
6 mins read

DTD (Document Type Definition) & Entities

PreviousNext Lesson
In Plain English

A DTD (Document Type Definition) defines the legal building blocks of an XML document. It specifies the list of permitted elements, child ordering, attribute types, and custom text entities. An XML document validated against its DTD is termed Valid XML.

The Mental Model

"A DTD is like a building permit inspector checklist: it lists exactly what rooms (elements), doors (attributes), and standard fixtures (entities) your house is legally allowed to possess."

Deep Dive: How It Works

<!DOCTYPE root [...]>: Encloses internal DTD rules or points to an external .dtd file.

<!ELEMENT name (child1, child2)>: Defines element hierarchy. (#PCDATA stands for Parsed Character Data).

<!ATTLIST element attr type default>: Declares attribute types (#REQUIRED, #IMPLIED, #FIXED).

<!ENTITY name "value">: Declares custom shortcut aliases expanded by XML parsers.

Syntax Blueprint

<!DOCTYPE note [
  <!ELEMENT note (to,from,body)>
  <!ELEMENT to (#PCDATA)>
]>

Specifies that <note> must contain exactly one <to>, <from>, and <body> in sequence.

PointCore Rules to Remember

Point star
Well-Formed vs Valid: Well-formed means correct XML syntax; Valid means it strictly adheres to a DTD or XSD schema.
Point star
Cardinality Symbols: + means 1 or more, * means 0 or more, ? means 0 or 1, no symbol means exactly 1.
Point star
Custom Entities: <!ENTITY author "Jane Doe"> allows using &author; in document text.

Common Beginner Traps & How to Fix Them

Mistake:Putting elements in the wrong order when DTD defines a strict sequence.

Why it happens: <!ELEMENT note (to,from)> fails validation if <from> appears before <to>.

How to fix: Ensure elements match the exact sequence declared in the DTD definition.

Pro Tip: While DTD was historically common, modern enterprise systems use XML Schema (XSD) because XSD is written in XML and supports rich data types.

Live Interactive Example

Hit Run Code to see it live
Valid Document with Internal DTD
XML 1.0 / XSD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Live Browser Preview
Click "Preview / Apply" to view the rendered output.
How it works: The memo element validates against the DTD with a custom &org; entity reference.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Write a DTD element declaration

  • PointDeclare <!ELEMENT user (name, email)> for a user containing parsed character data name and email.
1
2
3
4
5
6
7
8
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates