Markup Languages and their Grammars – SGML, DTD Resources
1. What is a markup language?
A markup language annotates text with tags that describe its structure or presentation. The text is the content; the tags are the markup.
| Kind of markup | Meaning | Example |
|---|---|---|
| Presentational | How it looks | <b>, <font> (old HTML) |
| Structural / procedural | What role each part plays | <h1>, <table> |
| Semantic / descriptive | What the data means | <price>, <author> (XML) |
Why grammar? A markup language needs a grammar – rules that say which tags exist, how they nest, which attributes they carry – so that documents can be validated and processed by programs. SGML and XML use a DTD for this grammar.
2. Family tree of markup languages
- GML (IBM, 1969) → SGML (ISO standard 1986) → HTML (Tim Berners-Lee, application of SGML) and XML (W3C 1998, a simplified subset of SGML).
- XHTML = HTML re-written as well-formed XML.
3. SGML – Standard Generalized Markup Language
SGML is an ISO standard (ISO 8879:1986) meta-language: a language for defining markup languages. It does not fix any tag set; you describe your own in a DTD.
Features
- Separates content from presentation.
- Document structure is described by a DTD (grammar) and checked by a validating parser.
- Supports optional tag omission (e.g. closing tags), short references, entities, marked sections – powerful but very complex.
- Used for large technical documentation (aircraft manuals, legal and publishing industries), and is the ancestor of HTML and XML.
Parts of an SGML document
- SGML declaration – character set, syntax.
- DTD – element/attribute grammar.
- Document instance – the actual marked-up text.
Limitations
Complex to implement, expensive tools, too heavy for the web → XML was designed to keep SGML's power but be simple.
4. HTML as an SGML application
HTML 2.0–4.01 were defined by SGML DTDs (loose, many tags fixed, optional end tags, presentation mixed with structure). HTML5 is no longer SGML-based (its own parsing rules).
5. SGML vs XML vs HTML
| Feature | SGML | HTML | XML |
|---|---|---|---|
| Type | Meta-language | Fixed markup language | Meta-language (subset of SGML) |
| Tags | User-defined via DTD | Predefined | User-defined |
| Closing tags | May be optional | Often optional | Mandatory |
| Case | Configurable | Insensitive | Case sensitive |
| Complexity | Very high | Low | Moderate |
| Purpose | Define document formats | Display web pages | Describe and transport data |
| Validation | DTD | Weak | DTD / XML Schema |
6. DTD – Document Type Definition
A DTD is the grammar of a document class: it lists the legal elements, their content models (children and order), attributes and entities. A document that follows its DTD is valid; one that only obeys basic syntax rules is well-formed.
Declaring a DTD
- Internal subset: inside the document,
<!DOCTYPE library [ ... ]>. - External subset:
<!DOCTYPE library SYSTEM "library.dtd">(private) orPUBLIC "-//ID//EN" "url"(public identifier).
Element declarations: <!ELEMENT name content-model>
| Content model | Meaning |
|---|---|
(#PCDATA) |
Parsed text only |
EMPTY |
No content (` |
| `) | |
ANY |
Any content |
(a, b) |
Sequence: a then b |
(a | b) |
Choice: a or b |
x? |
zero or one |
x* |
zero or more |
x+ |
one or more |
Attribute declarations: <!ATTLIST element attr type default>
- Types:
CDATA,ID,IDREF,NMTOKEN, enumerated(en|hi). - Defaults:
#REQUIRED,#IMPLIED(optional),#FIXED "v", or a default value.
Entities: <!ENTITY pub "BEU Press">; used as &pub;. Parameter entities (%name;) are reusable DTD fragments.
Worked example (validated with xmllint)
<?xml version="1.0"?>
<!DOCTYPE library [
<!ELEMENT library (book+)>
<!ELEMENT book (title, author+, price?)>
<!ATTLIST book isbn CDATA #REQUIRED lang (en|hi) "en">
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ENTITY pub "BEU Press">
]>
<library>
<book isbn="978-1" lang="en">
<title>Web Technology</title>
<author>A. Kumar</author>
<price>350</price>
</book>
</library>
This document is valid. If a <name> element appears inside <book>, a validating parser reports "No declaration for element name".
Advantages of DTD
Simple, compact, built into SGML/XML, widely supported, supplies default attribute values and entities.
Limitations of DTD
Not written in XML syntax; no data types (everything is text); no namespaces support; weak constraints on counts (cannot say "exactly 3"). → Solved by XML Schema (Module 3).
7. "DTD resources"
A DTD can be reused and published as a resource:
- Public DTDs: XHTML 1.0 DTDs (strict, transitional, frameset), DocBook, TEI, NewsML.
- W3C hosts the HTML/XHTML DTDs; XML parsers fetch them using the
PUBLIC/SYSTEMidentifiers. - Catalogs map public identifiers to local copies to avoid network lookups.
- Tools:
xmllint --valid, Oxygen, JavaDocumentBuilderwith validation on.
Frequently Asked Questions
QWhat is a markup language?
A language that adds tags to text to describe its structure, presentation or meaning, e.g. HTML, XML, SGML.
QWhat is SGML?
Standard Generalized Markup Language, ISO 8879 (1986): a meta-language for defining markup languages through DTDs. HTML and XML derive from it.
QWhy is SGML not used directly on the web?
It is too complex and expensive to implement; XML was created as a simplified subset.
QWhat is a DTD?
Document Type Definition – a set of rules (grammar) defining the allowed elements, attributes, entities and structure of a document.
QDifference between well-formed and valid XML?
Well-formed: obeys XML syntax (one root, closed and properly nested tags, quoted attributes). Valid: well-formed and conforms to its DTD/Schema.
QWrite the DTD symbols `+`, `*`, `?`, `|`, `,`.
+ one or more, * zero or more, ? optional, | choice, , sequence.
QGive DTD for an element `student` with name, roll and optional email.
<!ELEMENT student (name, roll, email?)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT roll (#PCDATA)>
<!ELEMENT email (#PCDATA)>
QWhat are the limitations of DTD?
No data types, not XML syntax, no namespace support, limited occurrence control – hence XML Schema.
QDifference between internal and external DTD?
Internal DTD is inside the XML file; external DTD is in a separate .dtd file referenced with SYSTEM or PUBLIC.
QCompare SGML, HTML and XML.
SGML is a meta-language; HTML is one SGML application with fixed tags for display; XML is a simplified SGML subset for user-defined data tags (see table).