The converse to XML parsing is SXML pretty-printing: a translation from an abstract to a concrete XML syntax, to an XML or HTML document. The pretty-printing makes it possible to author and compose XML documents in their SXML form. SXML is more concise and expressive than a raw markup language. SXML representing regular Scheme code can be entered in any Scheme-sensitive editor. SXML as a data structure -- a list -- can likewise be composed as a literal or quasi-literal expression. Furthermore, SXML can be produced by regular Scheme functions, which may make authoring more succinct, advanced, and less tedious, as the code below illustrates.

The pretty-printing consists of two passes. The first one traverses an s-expression tree in post-order, and converts it into a tree of HTML fragments. The tree of fragments, when flattened and concatenated, yields the resulting HTML document. Of course there is no need to flatten the tree and concatenate strings explicitly. Rather, we traverse the tree of fragments depth-first and write out fragments. In this approach, all character and other data, once created, stay where they were originally allocated. No data are ever copied. Functions such as string->number are implied as well.

The second pass, a pre-order traversal of a tree of fragments, is performed by a function SRV:send-reply . Besides writing out character data it encounters, the function executes thunks and ignores '() and #f . This feature lets us, for example, embed conditional forms into an s-expression representing HTML:

(SXML->HTML `(p "par1" "par2" ,(and test (list "par3" "par4"))))

test

Here is a more meaningful example, which intelligently generates a "navigation bar", a pair of links to the preceding and the following slides. Obviously the first and the last page must have only one navigation link.

(define (print-slide n max-count) (SXML->HTML `((h2 "Slide number:" ,n) ; Note n is used in its native form ,(and (positive? n) `(a (@ (href "base-url&slide=" ,(- n 1))) "prev")) ,(and (< (+ n 1) max-count) `(a (@ (href "base-url&slide=" ,(+ n 1))) "next")) (p "the text of the slide"))))

SXML makes composing XML or HTML documents even more convenient by enabling higher-order "tags" -- just as LaTeX helps typeset documents by offering higher-order "macros". LaTeX macros are eventually expanded by TeX; SXML "tags" are evaluated by Scheme. The SXML specification is an example of such an advanced composition. The SXML.html web page that describes SXML is actually written in SXML itself.

*TOP*

(productions (production 1 (nonterm "TOP") ((sexp (term-lit "*TOP*") (ebnf-opt (nonterm "aux-list")) (ebnf-* (nonterm "PI")) (ebnf-* (nonterm "comment")) (nonterm "Element")))))

<table border=0 bgcolor="#f5dcb3"> <tr valign=top><td align=right><a name="prod-1">[1]</a> </td> <td align=right><code><TOP></code></td> <td align=center><code> ::= </code></td> <td align=left><code><strong>(</strong> <em>*TOP*</em> <aux-list>? <PI>* <comment>* <Element> <strong>)</strong></code> </td> </tr></table>

\begin{tabular}{rrcp{2.8in}} {[}1{]} & \texttt{<TOP>} & $::=$ & \texttt{\textbf{(} {\itshape *TOP*} <aux-list>? <PI>* <comment>* <Element> \textbf{)} } \\ \end{tabular} \\

Using SXML to express its grammar has another important advantage: you can easily write a transformation or an SXPath query on the whole SXML.scm to make sure that every nonterm mentioned on the right-hand of some production appears on the left-hand side of exactly one production. The SXML.scm code thus lends itself not only to a flexible presentation to a human but to a formal reasoning about as well.

As a matter of fact, the ability to treat SXML as a "code" or as "data" is used during SXML transformations. The expansion process of a tag can re-scan the SXML document, with a different stylesheet. That is how hierarchical tables of contents are generated. Unlike LaTeX, you do not need to write auxiliary files and do not need to re-run the document processor.

The definition of SXML->HTML is given below. The function also handles "higher-level" HTML tags, for example, html:begin . These tags make HTML code even more succinct and expressive, as the SXML code for this page clearly shows. This page is authored in SXML. Local and site links are generated automatically.