XML technologies / XML / XML in a Nutshell / XML as a document format / Transformation and presentation

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

Markup in a typical XML document describes its structure, not its presentation: how it is organized, rather than how it looks. Although XML documents are text and could simply be read as they stand, they are far more often converted to another format before being presented to people. A key idea behind markup languages generally, and XML in particular, is that the input format need not match the output format. In other words, what you see is neither what you get nor what you want to get. The input markup language serves the writer’s convenience; the output language serves the reader’s.

Naturally, this requires a way to transform input into output. Most XML documents undergo some transformation before presentation. They may be converted to another XML vocabulary such as XHTML or XSL-FO, or to a non-XML format such as PostScript or RTF.

XML’s semiofficial transformation language is Extensible Stylesheet Language Transformations (XSLT). An XSLT document contains a list of template rules. Each template rule has a pattern specifying the elements and other nodes it matches. An XSLT processor reads the input document. When it encounters something matching a template rule in the stylesheet, it outputs that rule’s template. The template can tell the processor which input content to insert in the output. This allows text to be retained while markup changes. For example, you could write a stylesheet converting DocBook documents to TEI documents.

XSLT is not the only transformation language for XML documents. Other stylesheet languages are available, such as Document Style Sheet and Semantics Language (DSSSL), as are numerous proprietary tools such as OmniMark. Most have particular strengths and weaknesses for certain document types. Custom programs written in many different programming languages, such as Java, C++, Perl and Python, can use many APIs such as SAX, StAX, DOM, JDOM and XOM to transform documents. This is sometimes useful when you want more than pure transformation—for example, interpreting particular elements as database queries and inserting their results into the output, or asking the user questions during transformation. When choosing a tool, however, your main guide should be the language and syntax you know best. De linguis non disputandum est.

A transformation has many possible output formats. A PostScript file can be printed on paper, transparencies, slides or even T-shirts. A PDF document can be viewed on all these media and on screen. For screen display, however, plain HTML is preferable to PDF: it is available on an extraordinary range of platforms and is easily generated from XML using XSLT. Creating PDF or PostScript normally requires an additional conversion step, in which special software converts an XML output format such as XSL-FO into what you actually want.

An alternative to transformation-based presentation is a descriptive stylesheet specifying how the original document’s elements should be formatted. This is the realm of Cascading Stylesheets (CSS). CSS works especially well for narrative documents requiring only a list of fonts, styles or sizes to apply to element contents. The crucial point is that removing markup leaves more or less the plain-text version of what you want to see. No reordering or rearrangement is needed. This approach works less well for record-like documents whose bare content may be an undifferentiated pile of numbers, dates or other information that is unintelligible without markup’s context and annotations. A combined approach is more practical there: first a transformation creates a new document with rearranged, annotated information; then a CSS stylesheet applies style rules to its elements.

  

 

 

 

back to top