XSLT template rules vs. Lisp macros

In bed this morning, I was contemplating all I learned about Lisp last night. XSLT’s most obvious analog to Lisp macros is its ability to generate stylesheet code, since XSLT is in XML syntax.

But if data is really code, then XSLT template rules are also like macro definitions. An element in the source document is processed and thereby expanded, causing the code in the body of the corresponding template rule to be executed.

At one level, template rules seem more powerful than Lisp macros. A macro is defined by a name and resolved via name-lookup, whereas a template rule is defined by a pattern and resolved via pattern-matching. I don’t think I’ve encountered this level of indirection yet in my two days of learning Lisp. I’m curious what the best analog to this pattern-based function resolution is in Lisp. Maybe the best analog is to be found in particular Lisp functions, such as remove-if-not. Perhaps XSLT’s core processing model is like just another Lisp function.

An obvious difference is that Lisp macros are expanded recursively, whereas template rules are just applied once. I’ve wanted a way to apply them recursively sometimes (well, certain template rules in my stylesheet if not all of them). This isn’t built in to XSLT. Either you have to store a temporary result in a variable and then process the results again, or you have to define a pipeline process outside of XSLT to invoke the XSLT processor. I’ve contemplated how a more automatic recursive application of template rules might look or might be built into an XSLT-like language.

Anytime you learn something new, you’re going to compare it to what you already know. So far, it seems like having an XSLT background is going to be helpful in my learning of Lisp.


  1. Slava Pestov said,

    October 3, 2006 @ 12:43 pm

    Lisp macros (at least in Common Lisp) are procedural — you write code which manipulates code — XSLT has a much more declarative feel. You might want to take a look at Scheme’s ‘hygienic macros’ and define-syntax form. It provides the same functionality as CL’s macros but it feels more like XSLT.

  2. pkhuong said,

    October 4, 2006 @ 5:24 am

    The problem with pattern matching dispatch and fixpointing (repeated) application is that it’s very easy (imo) to have a hidden infinite loop. Pattern matching also has the problem that rules can be ambiguous, but there are various workarounds.

  3. How can I do this in XSLT? | cartesian product said,

    May 16, 2011 @ 12:00 pm

    [...] Evan Lenz ” XSLT template rules vs. Lisp macros (evanlenz.net) [...]

RSS feed for comments on this post · TrackBack URI

Leave a Comment