leastfixedpoint

xxexpr.ss - an SXML-to-XML converter

This page is a mirrored copy of an article originally posted on the LShift blog; see the archive index here.

The SSAX XML parsing- and processing-library provides robust, high-quality XML reading tools for Scheme, but doesn’t include a general purpose XML writer. Over the past couple of years, a few of my projects have had a need to convert SXML-like data to an XML 1.0 external representation, and so I’ve written a portable SXML-to-XML printing library (both a snapshot and a darcs repository). The library has been used with Chicken, MzScheme, and SISC, and currently includes module wrappers for Mzscheme and SISC (or other psyntax-based Schemes).

The library is parameterized over a choice of double- or single-quotes for attribute printing, and can, if required, be instructed to use explicit close-tags when an empty-tag is encountered. It provides procedures for producing a string representation of an XML fragment, for printing an XML fragment directly to a port, and for pretty-printing an XML fragment with indentation. For example,

(pretty-print-xxexpr
 (let ((title "My Page"))
   (list
    '(*pi* xml (version "1.0"))
    `(html (head (title ,title))
           (body (h1 ,title)
                 (p "Hello, world!"))))))

produces the following output (don’t mind the invalid XML PI, that’s something WordPress is doing - the actual output from the library is well-formed!):

< ?xml version='1.0'?>
<html>
    <head>
        <title>My Page</title>
    </head>
    <body>
        <h1>My Page</h1>
        <p>Hello, world!</p>
    </body>
</html>

You can either browse the code, or retrieve it using

$ darcs get http://www.lshift.net/~tonyg/xxexpr/