XML technologies / XML / Perl & XML / Introduction to XML / Well-formed documents and free-form XML
(Excerpt from "Perl & XML" by Erik T. Ray & Jason McIntosh)
XML’s grandfather SGML required every element and attribute to be described by a correct declaration in the DTD. We explain this in more detail in the next section. For now, imagine filling in a form rather than writing a document. This form considerably increases the SGML processor’s workload, a major reason SGML did not become established on the Internet. HTML, originally developed as an SGML application, was also hampered by this requirement because every valid HTML document had to conform to the HTML DTD. Extending the language required a committee’s approval.
XML sets these constraints aside and permits free-form XML. In this mode, an XML document needs to meet only minimal syntax rules to be acceptable. An XML document following these minimal rules is called well-formed. These relaxed checks are wonderfully liberating for programmers. You do not always need to read the DTD just to make a small change. The processor only checks the simplest requirements.
In free-form XML, you choose element names yourself instead of following a fixed vocabulary as in HTML. Taking liberties with structure is risky, but if you know what you are doing, nobody objects. If you distrust the markup, however, introduce element and attribute declarations to enforce a structure, as described in the next section.
What are these minimal rules? Here is a brief selection, without claiming complete precision:
A document may have only one top-level element, the document element or root element. All other elements and text data must be inside it. The XML declaration and the document type declaration must precede it, however.
Every element with content must have a start tag and an end tag.
Element and attribute names are case-sensitive. Besides letters, only a few other characters are permitted: digits, underscores, hyphens, and periods. The first character must be a letter or underscore. Colons are also permitted, but only to separate a namespace prefix.
All attributes must have values enclosed in single or double quotes.
Elements must never overlap. If an element’s start tag is inside another element, its end tag must also be inside it.
Certain characters, especially less-than and greater-than signs (
<>) and the ampersand (&), are reserved for markup and prohibited elsewhere. To use them in text data, you must use entity references or CDATA sections.Empty elements must be identifiable from their syntax. A slash (
/) is placed before the closing greater-than sign (>) of the start tag.
There are further rules. For a deeper understanding of well-formedness you should therefore read a Introduction to XML or consult the official W3C specifications at http://www.w3.org/XML.
Ensure that a document is well-formed whenever you want to process it with XML processors. In a sense, XML that is not well-formed does not exist. You can check this with a dedicated tool called a well-formedness checker, a kind of XML parser that reports errors when necessary. Its output is often very precise, normally giving the exact line and column of a problem. In XML basics: reading and writing we will examine checkers and parsers in more detail.
Copyright © 2003 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from the book "Perl & XML" is subject to the same conditions as the printed edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de
back to top