What XML Has to Offer

(Excerpt from "XML in a Nutshell" by Elliotte Rusty Harold & W. Scott Means)

XML is a meta-markup language for text documents. Data is included in XML documents as text strings, surrounded by text markup that describes the data. A basic XML unit of data and markup is called an element. The XML specification defines the precise syntax this markup must follow: how tags delimit elements, what tags must look like, which names are suitable for elements, where attributes are placed, and so on. Superficially, the markup in an XML document looks much like the markup in an HTML document. There are, however, some crucial differences.

Most importantly, XML is a meta-markup language. This means there is no fixed set of tags and elements that works everywhere, all the time. Any attempt to create a finite set of such tags is doomed to fail. Instead, XML allows developers and authors to invent elements as needed. Chemists can use elements that describe molecules, atoms, bonds, reactions and other concepts encountered in chemistry. Estate agents can use elements that represent flats, rents, commissions, locations and other concepts needed in the property business. Musicians, in turn, can use elements that describe quarter notes, half notes, clefs and other objects commonly used in music. The X in XML stands for eXtensible. Extensible means that the language can be extended and adapted to meet many different needs.

Although XML is very flexible about which elements may be defined, it is very restrictive in many other respects. The XML specification defines a grammar for XML documents that determines where tags may appear and what they must look like, which element names are permitted, how attributes are associated with elements, and so on. This grammar is precise enough to allow the development of XML parsers that can read any XML document. Documents that conform to this grammar are called well-formed. Documents that are not well-formed are no more acceptable than a C program containing a syntax error. Programs that process XML reject documents that are not well-formed.

To make documents easier to exchange and process, people or organisations sometimes agree to use only particular tags. These sets of tags are called XML applications. An XML application is not a software application that uses XML, such as Mozilla or Microsoft Word. Rather, it is an application of XML to a particular domain, such as vector graphics or cooking.

The markup in an XML document describes the document’s structure. It allows you to determine which elements are related to which other elements. In a well-designed XML document, the markup also describes the document’s semantics. For example, markup can indicate that an element is a date, a person or a barcode. In well-designed XML applications, the markup says nothing about how the document should be displayed. In other words, it does not specify whether an element is bold, italic or a list item. XML is a structural and semantic markup language, rather than a presentation language.

Note: Some XML applications, such as XSL Formatting Objects (XSL-FO), are designed to describe the presentation of text. These are exceptions that prove the rule, however. XSL-FO does indeed describe presentation, but you would never write an XSL-FO document directly. Instead, you write an XML document with a more semantic structure and then use an XSL transformation stylesheet to convert that structure-oriented document into presentation-oriented XML.

The markup permitted in a particular XML application can be specified in a schema. Individual document instances can be checked against that schema. Documents that conform to the schema are called valid. Documents that do not are invalid. Validity always refers to a schema: whether a document is valid or invalid depends on the schema against which you check it. Not all documents need to be valid. For many purposes, it is enough for a document to be well-formed.

There are many different XML schema languages, with significantly different expressive capabilities. The most widespread schema language, and the only one defined within the XML 1.0 specification itself, is the DTD (Document Type Definition). A DTD lists the permitted markup and specifies where and how it may be used in a document. DTDs are optional in XML. They are not necessarily sufficient in every case, either. DTD syntax is limited and does not allow you to make useful statements such as “This element contains a digit” or “This string is a date between 1974 and 2032”. The W3C’s XML Schema language (often confusingly referred to simply as Schema) does allow such constraints. Besides these two, you can use other schema languages, including RELAX NG, Schematron, Hook or Examplotron, and this list is far from exhaustive.

All currently existing schema languages are purely declarative. Some constraints, however, can only be expressed in a Turing-complete programming language. For example, if an XML document contains an order, you need a Turing-complete language to multiply the Einzelpreis of each Bestellartikel by its Anzahl , add up the line items and compare the result with the value of the <Zwischensaldo> element. Today’s schema languages are also unable to check constraints outside the document, such as the requirement that “Each <SKU> element matches the SKU field of a record in a product table in the inventory database.” If you write programs to read XML documents, you may have to add code to check such assertions, just as you would when writing programs to read a text file whose entries are supposed to be separated by tabs. The difference is that XML parsers present the data in a much more convenient format and do much of the work for you, so you need to write less code yourself.

   

<< backnext >>