SENG1050 Chap.3 XML Documents, Parsing and DTD Validation
XML Documents, Parsing and DTD Validation
This chapter follows the Week 3 lecture, which the exam review stars for document structure, the tree and the DTD. It explains why XML separates data from structure, how a well-formed document is written, how a parser treats parsed and unparsed data, and how a document is drawn as a tree. It then writes element and attribute declarations with their modifiers and defaults, and tests fragments for validity against a DTD.
The matching lab tags plain text as XML, adds an internal DTD and validates the result with a checker that reads the DTD.
What this chapter covers
- 01
Why separate data from HTML
- 02
XML versus HTML and databases
- 03
Declaration, root and naming rules
- 04
Drawing an XML tree
- 05
PCDATA, CDATA and entities
- 06
Internal and external DTDs
- 07
ELEMENT declarations and modifiers
- 08
ATTLIST types and defaults
Worked example · free
Draw the XML tree for a menu document
- 1Draw the single root, menu, at the top in a rounded box, because every XML document has exactly one root element.
- 1Hang the two dish elements beneath it, each in a rounded box, and attach type="veg" to the first dish in a plain rectangle, the shape for attributes.
- 1Under the first dish draw name and price; under the second dish draw name.
- 1Put the data Pumpkin soup, 12.50 and Fish tacos in slanted boxes below their elements, the shape the course uses for data.
Key terms
- XML declaration
- The XML declaration is the first line of an XML file, naming the version and character encoding.
- Root element
- The root element is the single element that contains every other element in an XML document.
- Well-formed document
- A well-formed document obeys XML syntax rules and forms one tree under a single root.
- Valid document
- A valid document is well-formed and also conforms to its associated DTD.
- CDATA section
- A CDATA section holds raw text that the parser does not read as markup.
- Document Type Definition
- A Document Type Definition lists the legal elements, their nesting and their attributes for a class of XML documents.
- Entity
- An entity is a named shortcut for text or a special character, written as &name; in the document.
- Mixed content
- Mixed content is an element model that allows text and child elements together, declared as (#PCDATA | child)*.
XML Documents, Parsing and DTD Validation FAQ
What is the difference between a well-formed and a valid XML document?
A well-formed document follows the syntax rules: one root, every element closed, correct nesting, quoted attribute values and case-consistent names. A valid document is well-formed and also matches its DTD, so the right elements appear in the right order with the right attributes.
What do the DTD symbols plus, star and question mark mean?
Plus means one or more, star means zero or more and question mark means zero or one. No symbol means exactly one. Commas fix the order of children, and a vertical bar offers a choice between them.
Why does my XML fail when it contains an ampersand?
The parser reserves the ampersand for entities and the less-than sign for tags. Replace each one with its predefined entity reference, or place the raw text inside a CDATA section so the parser leaves it alone.
When is XML a better choice than a database?
The lecture favours XML for transmitting data, for small data sets, for saving disk space and for simple management without a query language. A database is better for large or frequently changing data, many users writing at once, and access control.
Where must the XML declaration go?
It must be the very first thing in the file, before any comment, blank line or element. It names the XML version and character encoding, and it has no closing tag because it is not part of the element tree.
Exam move
Take any small data set, such as a timetable or a shopping list, and write it as XML, then draw its tree using the three shapes. Next write an internal DTD for it, using each modifier at least once, and test two fragments of your own against it, one valid and one invalid. Always validate against the DTD rather than checking well-formedness alone.
Before the quiz, rehearse naming each declaration part aloud: element name, content model, modifier and attribute default.
Working through XML Documents, Parsing and DTD Validation in SENG1050? Sia is AskSia’s AI Computer Science tutor — ask any SENG1050 XML Documents, Parsing and DTD Validation question and get a clear, step-by-step explanation grounded in how SENG1050 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.