XML technologies / XML / Python & XML / XML definitions
(Excerpt from “Python & XML” by Christopher A. Jones & Fred L. Drake, Jr.)
This page contains detailed definitions from the XML specification. These technical terms are often used in connection with DOM and with XML technology in general. All terms are defined here for reference. This appendix should accompany the specification overview in XML basics. To extract the XML terms and definitions from the specification, we thought it appropriate to write an XML-processing script operating on the XML version of the W3C Recommendation. Use the following program as a useful example of discovering the benefits of Python and XML and extracting terms from the XML version of the specification.
Example: gen-td.py — a script for outputting XML definitions
"""
Erzeuge HTML für Begriffe und Definitionen direkt aus der XML-Spezifikation.
XML-Quelle muß von der Standardeingabe kommen.
"""
import sys
from xml.sax import ContentHandler
from xml.sax import make_parser
class XMLSpecHandler(ContentHandler):
"""
Diese Klasse implementiert einen Teil der SAX-API, um Begriffsdefinitionen
aus der Quelldatei der XML-Spezifikation herauszuziehen.
"""
inTermDef = 0
def startElement(self, name, attrs):
if name == "termdef":
self.inTermDef = 1
self.strTermDefContents = ""
print "<p><b>" + attrs.get(’term’, "") + "</b><br>"
def characters(self, ch):
if self.inTermDef:
self.strTermDefContents += ch
def endElement(self, name):
if name == "termdef":
self.inTermDef = 0
self.strTermDefContents += "</p>"
print self.strTermDefContents
# Hauptprogramm
if __name__ == "__main__":
dh = XMLSpecHandler( )
parser = make_parser( )
parser.setContentHandler(dh)
print "<html><head>"
print "<style type=’text/css’>"
print "body { font-family: sans-serif; }"
print "</style>"
print "</head><body>"
parser.parse(sys.stdin)
print "</body></html>" You can download the XML specification as an XML document from the W3C website (W3C: All Standards and Drafts: XML). Then run the script in the example above to obtain a very simple HTML file:
$[chris@spindle]> python gen-td.py < XMLSpec.py > termdef.htmlWhen you load the file termdef.html in your web browser, you will see all the term definitions from the XML specification in simple HTML. The following figure shows this file in a browser.
Note: Henning Behme and Stefan Mintert translated the XML specification into German (Extensible Markup Language (XML) 1.0). However, that version is available only in HTML and is therefore unsuitable for the script given here. The browser consequently displays the English original. —Translator’s note.
Figure: termdef.html in a browser
One remark: the designers of the XML version of the specification did not neatly enclose the complete text of each definition in the termdef tags. A little intervention was therefore necessary to present them accurately here. Moreover, these definitions are a subset of those in the specification: definitions referring only to the specification itself rather than XML were removed. The list was alphabetized for readability.
Definitions of XML terms
Attribute
The
Name-AttValuepairs are called an element’s attribute specifications.
Attribute default
If the declaration is neither
#REQUIREDnor#IMPLIED, theAttValuevalue contains the declared default value. The keyword#FIXEDstates that the attribute must always have the default value. If a default value is declared and an XML processor encounters an omitted attribute, it behaves as though the attribute were present with the declared default value.
Attribute-list declaration
Attribute-list declarations specify the name, data type and default value, if any, of every attribute associated with an element type.
Attribute name
The name in each pair is called the attribute name.
Attribute value
The content of
AttValue(the text between the delimiters ' or ").
CDATA section — CDATA section
May occur wherever character data is allowed; used to mark blocks of text containing characters that would otherwise be interpreted as markup.
Character
An atomic unit of text as specified by ISO/IEC 10646. Legal characters are tab, carriage return, line feed, and the legal graphic characters of Unicode and ISO/IEC 10646. These versions of the standards were current when this book was written. New characters may be added in amendments or new editions. XML processors must therefore accept any character in the range specified for Char. Use of compatibility characters is discouraged.
Character data
All text that is not markup constitutes the document’s character data.
Character reference — Character reference
Refers to a particular character in the ISO/IEC 10646 character set, for example one not directly available on input devices.
Comment
Comments may occur anywhere outside other markup in the document; they may also occur in the document type declaration wherever the grammar permits. They are not part of the document’s character data; an XML processor may, but need not, let an application access comment text. For compatibility, the string “
--” must not occur in comments.
Conditional section
Portions of the document type declaration’s external subset that are included in or excluded from the logical structure of the DTD according to their accompanying keyword.
Content
The text between the start-tag and end-tag.
Content model
In this case, the constraint includes a content model, a simple grammar governing the allowed types of child elements and the order in which they may occur.
Document entity — document entity
Serves as the root of the entity tree and as the starting point for an XML processor.
Document type declaration
The XML document type declaration contains or references markup declarations providing a grammar for a class of documents. This grammar is known as the document type definition (DTD). The document type declaration may point to an external subset, a special external entity containing markup declarations, contain markup declarations directly in an internal subset, or both. The document’s DTD comprises both subsets together.
Element
Every XML document contains one or more elements, delimited by start-tags and end-tags or, for empty elements, by an empty-element tag. Every element has a type identified by a name, sometimes called its generic identifier (GI), and may have a number of attribute specifications.
Element content
An element type has element content when its elements may contain only child elements, but no character data, optionally separated by whitespace (characters matching the nonterminal S).
Empty
An element with no content.
Empty-element tag
An empty-element tag has a special form:
<tagName/>.
End-tag
The end of every element beginning with a start-tag must be marked with an end-tag repeating the element type name given in the start-tag.
Entity
An XML document may consist of one or many storage units. These are called entities; all have content and all, except the document entity and external DTD subset, are identified by an entity name.
Entity reference
Refers to the content of a named entity.
Enumerated attribute values
Enumerated attributes may take a value from a list of values provided in the declaration.
Error
A violation of the XML specification’s rules; results are undefined. Conforming software may detect and report an error and may recover from it.
Escape
Entity references and character references may both escape the greater-than sign, ampersand and other delimiters. For this purpose, several general entities (
amp,lt,gt,apos,quot) are specified. Numeric character references may also be used; they are expanded immediately upon recognition and must be treated as character data, so numeric character references#60;and&may replace < and & when these occur in character data.
External entity
If the entity is not internal, it is an external entity.
External markup declaration
Defined as a markup declaration in the external subset or in a parameter entity, external or internal; the latter are included because non-validating processors need not read them.
Fatal error
An error that a conforming XML processor must detect and report to the application. Upon encountering a fatal error, the processor may continue processing data to look for further errors and report them to the application. To support error correction, it may make unprocessed data from the document, containing mixed character data and markup, available to the application. Once a fatal error has been detected, however, the processor must not continue normal processing: it must not continue passing character data and information about the document’s logical structure to the application in the normal way.
General entity
Entities used in document content. This specification sometimes calls general entities simply entity if there is no ambiguity.
General entity reference
References to parsed general entities use the ampersand (
&) and semicolon (;) as delimiters.
Include
An entity is included when its replacement text has been retrieved and processed in place of the reference as though it were part of the document at the point where the reference was recognized.
Internal entity replacement text
If the entity definition is an
EntityValue, the entity being defined is called an internal entity. There is no separate physical storage object; the entity’s content is provided in its declaration.
Literal entity value
The quoted string in the entity declaration corresponding to the nonterminal EntityValue.
Markup
Markup consists of start-tags, end-tags, empty-element tags, entity references, character references, comments, CDATA section delimiters, document type declarations, processing instructions, XML declarations, text declarations, and any whitespace at the top level of the document entity, outside the document element and not within other markup.
Markup declaration
An element type declaration, attribute-list declaration, entity declaration or notation declaration.
Mixed content
An element type has mixed content if its elements may contain character data, optionally interspersed with child elements.
Name
A token beginning with a letter or one of a few punctuation characters and continuing with letters, digits, hyphens, underscores, colons or periods. Together these are called name characters.
Notation
Identifies by name the format of unparsed entities, the format of elements with a notation attribute, or the application to which a processing instruction is addressed.
Notation declaration
Provides a name for the notation, used in entity and attribute-list declarations and attribute specifications, and an external identifier enabling an XML processor or its application to locate a helper program capable of processing data in that notation.
Parameter entity
Parsed entities used in the DTD.
Parameter entity reference
Parameter entity references use percent signs (
%) and semicolons (;) as delimiters.
Parent/child
For every non-root element C in the document, there is another element P such that C is in P’s content but not in the content of any other element within P’s content. P is called C’s parent element, and C a child element of P.
Process declarations
Although not required to check document validity, they must process every declaration they read in the internal DTD subset and in any parameter entities up to the first reference to a parameter entity they do not read. They must therefore use the information in these declarations to normalize attribute values, load internal entities’ replacement text and supply attribute default values.
Processing instruction — processing instruction
Processing instructions (PIs) allow documents to contain instructions for applications.
Public identifier
In addition to the system identifier (System Identifier), an external identifier may contain a public identifier.
Replacement text
The entity’s content after replacing character references and parameter entity references.
Root element
There is exactly one element, called the root or document element, no part of which appears in the content of any other element.
Start-tag
Marks the beginning of every non-empty XML element.
System identifier
The
SystemLiteralis called the entity’s system identifier. It is a URI reference, defined in IETF RFC 2396 and updated by IETF RFC 2732, intended to be dereferenced to obtain input for the XML processor and thus construct the entity’s replacement text.
Text
A parsed entity contains text, a sequence of characters that may represent markup or character data.
Text entity
A parsed entity’s content is called its replacement text; it is regarded as an integral part of the document.
Unparsed entity
An unparsed entity is a resource whose content may or may not be text. If it is text, it need not be XML. Every unparsed entity has an associated notation identified by name. Beyond requiring an XML processor to make the entity and notation identifiers available to the application, XML imposes no constraints on unparsed entities’ content.
Validity
An XML document is valid if it has an associated document type declaration and complies with its constraints.
Validity constraint
A rule applying to all valid XML documents. Violations are errors; validating XML processors must report them at the user’s option.
Validating processor
Must report, at the user’s option, both violations of the constraints expressed in DTD declarations and failures to comply with this specification’s validity constraints.
Well-formed
A text object is a well-formed XML document if and only if:
taken as a whole, it matches the production named “document”;
it meets all the specification’s well-formedness constraints;
all parsed entities referenced directly or indirectly in the document are well-formed.
Well-formedness constraint
A rule applying to all well-formed XML documents. Violations of well-formedness constraints are fatal errors.
XML declaration — XML declaration
XML documents should begin with an XML declaration specifying the XML version used.
XML document
A data object is an XML document if it is well-formed as defined in this specification. A well-formed XML document may additionally be valid if it meets certain further constraints.
XML processor
A software module used to read XML documents and access their content and structure.
Copyright © 2002 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “Python & XML” 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