Summary of Introduction to XML: Syntax and Validation
Introduction to XML: Syntax and Validation for Students
Introduction
DTD (Document Type Definition) is a standard way to describe the structure and rules for XML documents. This material will explain DTD placement, basic constructs for declaring elements, attributes, and entities, occurrence quantifiers, and common errors in DTD design. The aim is to provide clear and practical information suitable for university study.
Definition: A DTD is a set of rules describing allowed elements, their order, attributes, and other constraints within an XML document. It is used to validate and ensure the consistency of XML data.
DTD Placement
Placement Options
- Internal DTD: DTD written directly within the XML file, after the XML declaration.
- External DTD: DTD referenced via a SYSTEM or PUBLIC identifier.
- Hybrid DTD: External DTD with embedded local additions within the document.
Example: Internal DTD
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE person [
<!ELEMENT person (name, profession*)>
<!ELEMENT name (firstname, surname)>
<!ELEMENT firstname (#PCDATA)>
...
]>
<person>
...
</person>
Example: External DTD
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE person SYSTEM "path/to/file.dtd">
<person>
...
</person>
- SYSTEM is used for non-public standards.
- PUBLIC is used for public (shared) DTDs, e.g., for formats like RSS or SVG.
Example: Hybrid DTD (external + local extensions)
<!DOCTYPE person SYSTEM "link.dtd" [
<!-- local additions and declaration overrides -->
<!-- <!ELEMENT person (name, profession*)> -->
...
]>
<person>
...
</person>
## Element Declarations
### Basic Syntax
- General form: <!ELEMENT element_name (content)>.
- Content can be text (#PCDATA), other elements, or a combination.
> Definition: #PCDATA refers to parsed character data that may contain entities and will be processed by an XML parser.
Examples:
- Element with only text content: <!ELEMENT phone (#PCDATA)> for <phone>123</phone>
- Element with two child elements: <!ELEMENT name (firstname, surname)>
- Empty element: <!ELEMENT image EMPTY>
- Any content: <!ELEMENT page ANY>
### Element Content Types
- **#PCDATA**: parsed character data without nested elements.
- **CDATA**: unparsed character data (unparsed) — suitable for embedding code (HTML, JavaScript) — in DTDs, CDATA typically appears for attributes, not as an element type.
## Element Occurrence Qualifiers
- `*` — zero or more occurrences
- `+` — one or more occurrences
- `?` — zero or one occurrence
- no qualifier — exactly one occurrence
Example: <!ELEMENT university (building+, address, student*, brewery?)>
- building+ means at least one <building> element
- address exactly one
- student* zero or more
- brewery? zero or one
### Examples of Invalid Order or Count
- Missing required address element: invalid.
- More addresses than allowed: if the DTD requires exactly one address, two addresses will cause an error.
- Incorrect order: the order of elements is significant in the DTD, i.e., <building>, then <address>, etc.
Interesting fact: In DTDs, the order of elements is significant, and the parser considers documents with a different arrangement invalid, even if they make logical sense.
## Attributes: Declaration and Types
### Basic Form
- Syntax: <!ATTLIST element_name attr_name type default_decl>
- Multiple attributes for an element can be declared within a single <!ATTLIST> section.
Example:
<!ATTLIST img src CDATA #REQUIRED
width CDATA #REQUIRED
height CDATA #REQUIRED>
### Available Attribute Types
- CDATA — general text
- Enumeration — a list of allowed values, e.g., (january | february | ...)
- ID — unique identifier within the document
- IDREF — reference to an existing ID
- IDREFS — multiple references (space-separated)
- NMTOKEN — valid XML name without spaces
- NMTOKENS — multiple NMTOKEN values separated by spaces
- ENTITY, NOTATION — special types for external entities and notations
> Definition: An ID is an attribute whose
Already have an account? Sign in
DTD and XML Declarations
Klíčové pojmy: A DTD defines the structure of an XML document., A DTD can be inline, external, or a combination of both., <!ELEMENT> defines elements and their content., #PCDATA stands for parsed character data., Qualifiers like *, +, and ? specify the number of occurrences., <!ATTLIST> defines attributes and their types., ID is a unique value, while IDREF refers to an ID., Entities allow for repeated strings or external resources., An enumeration restricts an attribute to a predefined list of values., NMTOKEN/NMTOKENS are used for named tokens without spaces., External DTDs increase the risk of XXE attacks., Validation tools help detect errors.