Web Basics · Web and Internet Technology

Markup Languages and their Grammars - SGML, DTD Resources

Watch lectures

Markup Languages and their Grammars – SGML, DTD Resources

SVGVector Architecture Diagram (SVG)
WEB & INTERNET TECHNOLOGY · Module 1★ High PriorityMarkup: SGML & DTDBEU CSE Sem 6 · Code 105614 · Web BasicsMNEMONICSGML is the parent; HTML and XML are its children.Well-formed = obeys the syntax. Valid = also obeys the DTD.Core BreakdownMarkupTags added to text to describe structure, look or meaning.Types: presentational (b), structural (h1), semantic (price).SGMLISO 8879 (1986) meta-language for defining markup languages.Powerful but complex; HTML 2 to 4.01 and XML descend from it.XML, HTMLXML is a simplified SGML subset with user-defined tags (W3C 1998).HTML is one fixed tag set made for display on the web.DTDGrammar of a document: allowed elements, attributes and entities.Internal subset inside the file, or external via SYSTEM or PUBLIC.LimitsDTD is not XML syntax, has no data types and no namespace support.XML Schema fixes these (Module 3).Diagram: Family TreeSGML ISO 8879, 1986HTML 1991, fixed tagsXML 1998, user-defined tagsXHTML, SVG, RSS XML applicationsELEMENTtags + modelATTLISTattributesENTITYshortcutsDTD symbols: + * ? | , ( )⚠️ Exam Traps & Viva Favorites① Well-formed or valid?Well-formed: one root, closed and nested tags,quoted attributes. Valid: well-formed and alsofollows the DTD or Schema.② What do + * ? mean in a DTD?+ one or more, * zero or more, ? optional.| is a choice and comma is a sequence.(#PCDATA) means parsed text only.③ Is HTML5 still SGML-based?No. HTML 2 to 4.01 were SGML applications, butHTML5 has its own parsing rules and no DTD.Tip: write one small DTD from memoryMaster Revision Table(learn it row by row!)SymbolMeaningExampleRemark#PCDATAParsed text only(#PCDATA)Used for leaf elementsEMPTYNo content<!ELEMENT br EMPTY>Like the br tag+One or morebook+At least one*Zero or moreauthor*Optional repeat?Zero or oneprice?Optional once#REQUIREDAttribute must appearisbn CDATA #REQUIRED#IMPLIED = optionalQUICK FACTSSGML ISO 8879 (1986) · HTML 1991 · XML 1998 (W3C)DOCTYPE: internal [ ... ] subset, or external SYSTEM / PUBLICxmllint --valid file.xml checks a document against its DTDVERIFIED★ Golden rule: the DTD is the grammar,an XML document is a sentence in it.

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

SVGVector Architecture Diagram (SVG)
SGML (ISO 8879, 1986)meta-language HTML (1991)XML (1998)SVG, XHTML, RSS XSL, XSD, XLink, SOAPMathML, WSDL, XAML
  • 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

  1. SGML declaration – character set, syntax.
  2. DTD – element/attribute grammar.
  3. 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) or PUBLIC "-//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".

MermaidDiagram
Rendering diagram…

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/SYSTEM identifiers.
  • Catalogs map public identifiers to local copies to avoid network lookups.
  • Tools: xmllint --valid, Oxygen, Java DocumentBuilder with 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).

Free AppAndroid App

Coursify Android App

Generate AI courses, take quick quizzes between classes, and master complex engineering concepts on the go.

← All Web and Internet Technology modules