XML technologies / XML / XML in a Nutshell / XML basics / Elements, tags and character data
(Excerpt from “XML in a Nutshell” by Elliotte Rusty Harold & W. Scott Means)
The document in A very simple but complete XML document contains a single element of type person. It is enclosed by the start-tag <person> and end-tag </person>. Everything between an element’s start-tag and end-tag, excluding tags, is its content . Here that is the text:
Alan Turing
The whitespace is part of the content, though many applications ignore it. <person> and </person> are markup. The string Alan Turing and surrounding whitespace are character data. Tags are the commonest markup, but other forms follow later.
Tag syntax
XML tags resemble HTML. Start-tags begin with < and end-tags with </. Both then have the element name and close with >. Unlike HTML, XML lets you invent tags. To describe a person, use <person> and </person>. For a calendar, use <kalender> and </kalender>. Names generally reflect content, not its formatting.
Empty elements
For empty elements with no content, special syntax uses one tag beginning with < and ending with />. For example, in XHTML, an XML reformulation of HTML, line breaks and horizontal rules are written <br /> and <hr /> rather than <br> and <hr>. They are equivalent to <br></br> and <hr></hr>. Choose either form. Start-tags alone, such as <br> or <hr>, are forbidden in XML and XHTML, unlike HTML, violating well-formedness.
Case sensitivity
Unlike HTML, XML is case-sensitive. <Person> is not the same as <PERSON> or <person>. Opening with <person> does not permit closing with </PERSON>. Use uppercase, lowercase or both, consistently within an element.
XML trees
Consider a more complex document. The next example has an element person whose information is marked to show meaning.
<person>
<name>
<vorname>Alan</vorname>
<nachname>Turing</nachname>
</name>
<beruf>Informatiker</beruf>
<beruf>Mathematiker</beruf>
<beruf>Kryptograph</beruf>
</person>Code example: A more complex XML document describing a person
Parents and children
The example still contains one person element, now with more than undifferentiated text. It has four child elements: one name and three beruf elements. The name element has two children, vorname and nachname.
The element person is also the parent of the name element and the three beruf elements. The element name is parent to vorname and nachname. The name element and three beruf elements are also called siblings (siblings). The elements vorname and nachname are siblings too.
Like people, elements can have several children. Unlike people, every child has exactly one parent, not two or more. Every element, with one exception shortly explained, has exactly one parent. It is completely enclosed in another element. If its start-tag is inside an element, its end-tag must be too. Overlapping tags such as <strong><em>oft gesehenes Beispiel aus HTML</strong></em> are forbidden. Since em starts inside strong, it must end there too.
The root element
Every document has one parentless element, first in the document and containing all others. In the first two examples, person fills this role. It is called the root element and sometimes the document element. Every well-formed document has exactly one root element. Since elements cannot overlap and each nonroot has one parent, XML forms a tree. The figure shows this for the preceding example: gray boxes are elements, black boxes character data, and arrows containment relationships.
Figure: Tree diagram for “A more complex XML document describing a person”
Mixed content
In that example, elements vorname, nachname and beruf contained character data with no tags. Elements person and name contained children and some whitespace, usually ignored. Separating text-only and child-only elements is common in record-like data. XML also serves freer narrative documents—business reports, magazine articles, term papers, short stories and web pages—as shown next.
<biographie>
<absatz><name><vorname>Alan</vorname> <nachname>Turing</nachname></name> war einer der ersten Menschen, der wirklich die Bezeichnung <betonung>Informatiker</betonung> verdiente. Seine Arbeiten auf diesem Gebiet können nicht alle aufgezählt werden, am bekanntesten sind der nach ihm benannte <betonung>Turing-Test</betonung> sowie die <betonung>Turing-Maschine</betonung>.</absatz>
<definition>Der <begriff>Turing-Test</begriff> ist bis heute der Standardtest, um festzustellen, ob ein Computer wirklich intelligent ist. Diese Prüfung wurde bisher noch nicht bestanden.</definition>
<definition>Eine <begriff>Turing-Maschine</begriff> ist ein abstrakter endlicher Automat mit unendlichem Speicher, der als äquivalent zu jedem anderen endlichen Automaten mit beliebig großem Speicher angesehen werden kann. Das heißt, was für eine Turing-Maschine gilt, gilt auch für alle Turing-Maschinen, unabhängig davon, wie sie implementiert wurden.</definition>
<absatz><name><nachname>Turing</nachname></name> war außerdem ein anerkannter <beruf>Mathematiker</beruf> und <beruf>Kryptograph</beruf>. Mit seiner Hilfe gelang es den Alliierten, den deutschen Enigma-Code zu entschlüsseln. Am <datum><tag>7.</tag> <monat>Juni</monat> <jahr>1954</jahr></datum> beging er Selbstmord, nachdem er wegen seiner Homosexualität verurteilt worden war und gezwungen wurde, weibliche Hormone einzunehmen.</absatz>
</biographie>Code example: A narrative XML document
Its root is biographie. biographie contains children absatz and definition, plus whitespace. Elements absatz and definition have further elements, such as begriff, betonung, name and beruf, plus unmarked character data. Elements such as absatz and definition with children and non-whitespace text have mixed content (also called mixed content). Such mixed content is common in articles, essays, stories, books, novels, reports and web pages—in short, narrative documents. It is uncommon and harder to process in machine-generated XML for database exchange, object serialization or persistent file formats. XML’s easy adaptation to these very different human and computer needs is a strength.
Copyright © 2005 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XML in a Nutshell” is subject to the same conditions as the bound edition: the work, including all its parts, is 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