Stupid XSL Trick

As Chris observed, and I pointed out in previous entries, I've been immersing myself in LISP lately. I speculated out loud that the structure of an XML document is equivalent to an s-expression. Chris responded by creating a LISP program that converts an s-expression into an HTML document.

I wondered if it would be possible to produce an XSL transformation that could convert XML into an s-expression; to use Chris's example, a transformation that converts this:


<html>
    <head>
        <title>Hello world</title>
    </head>
    <body>
        <blink>
            <span style="color:red">Testing.</span>
            Still blinking.
        </blink>
    </body>
</html>

Into this:


'(html
  (head
    (title "Hello world"))
  (body
    (blink
      (span (attributes (style "color:red")) "Testing.")  "
            Still blinking.
        ")))

What I came up with is an XSL transformation that can transform an arbitrary XML document into an s-expression. (It transformed the HTML above.) It still has a few problems: it can transform qualified names, but the colon-separated identifies cause problems for LISP, as it looks for a matching package. Also, deliberately empty text nodes are problematic. Otherwise, though, it works pretty well.

The transformation: sexp.xsl
The command line: $ xsltproc sexp.xsl sexp.xsl
The result: sexp.lsp

Tue, 17 Apr 2007 23:48

Comments: 0

Reader Comments

Add a Comment

  • Only the name field is mandatory. It's hard to keep track of a conversations where everyone is Anonymous.
  • If you choose to leave an email address, it will not be shown on the page.
  • A limited subset of HTML is allowed in your comment.

Name

Email

URL


Comment: (Limited HTML allowed.)