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:

I Always Meant To

When I was growing up, I wanted to read The Lord Of The Rings, but I would pick it up, plow through a few (hundred) pages, then put it back down again. I knew it would be good for me. I believed it would be fun. But somehow my attention would wander to other things, and I would end up putting LotR down again until the next time the urge struck. It is my particular idiosyncrasy that I want to start a thing from the beginning and see it through to the end, so each time I picked it back up, I started with page 1 of Fellowship of the Ring, instead of page 222 of The Two Towers, where I left off.

In the intervening months, I would occasionally browse the various encyclopedia, compendia, even the appendices with fascination. Still, though by then I knew the story pretty well, it was not until I was in my thirties before I succesfully read LotR cover to cover.

I can think of a few other things in my life that have been similar. Reading Gödel, Escher, Bach: An Eternal Golden Braid is one. Learning LISP is another.

A week or two ago, I picked up GEB:EGB again, and I've made it farther than I have before. It's slow going, because I like to read in bed before going to sleep. GEB veritably demands one attack it in a fully alert state, pencil and paper ready to work the experiments, exercises, and demonstrations. (Try it! Argh. Thanks, Doug.) Still, I am undaunted.

Coincidentally, several resources for learning LISP hit the link aggregators last week, so I'm kind of enthused about that too. The thing I really need to pursue LISP is a project to use it on. One thought is to start over on the Project Euler problems. I solved several of these a while back using Python, and it was a lot of fun. Chris tackled them in LISP as a refresher (and, he said, because many problems were greatly simplified by LISP's bignum type).

Speaking of

(kill (make-list N :initial-element BIRD) 
    (< N (length stones)))

It would be interesting to use LISP to investigate the formal systems Hofstadter discusses in GEB.

Mon, 09 Apr 2007 22:00

Comments: