University of Newcastle · FACULTY OF COMPUTER SCIENCE

SENG1050 Chap.3 XML Documents, Parsing and DTD Validation

- one subject, every graph, every model, every mark
8 Chapters4-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 3 of 12 · SENG1050

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.

In this chapter

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

Q [4 marks]. Draw the tree for this document: a root menu holding two dish elements; the first dish has the attribute type="veg" with a name Pumpkin soup and a price 12.50, and the second dish has a name Fish tacos. Use the course drawing convention. The mark allocation here is AskSia's own and is not a University marking scheme.
The menu document drawn as an XML tree: rounded boxes are elements, the square box is the type attribute and the slanted boxes hold data, so read nesting from tThe menu document drawn as an XML tree: rounded boxes are elements, the square box is the type attribute and the slanted boxes hold data, so read nesting from the single root downwards. menudishdishtype="veg" namepricename Pumpkin soup12.50Fish tacos
The menu document drawn as an XML tree: rounded boxes are elements, the square box is the type attribute and the slanted boxes hold data, so read nesting from the single root downwards.
  • 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.
The tree has menu at the root, two dish children, an attribute box type="veg" on the first dish, name and price under the first dish, name under the second, and the three data values in slanted boxes at the leaves.
Sia tip — Draw attributes and data in their own shapes, never inside the element box: rounded for elements, rectangles for attributes, slanted boxes for data.
Glossary

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)*.
FAQ

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.

Study strategy

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.

A+Everything unlocked
Unlocks this Bible + all 14 of your University of Newcastle subjects - and 1,000+ Bibles across every Australian university.
Sia - your SENG1050 tutor, unlimited, worked the way the exam marks it
The full 4-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works