Transforming Coldfusion code with XSLT

Note: For those looking for something useful, you won't find it in this post.

For a little Saturday fun, I set out to transform Coldfusion code using XSLT. It didn't take long before I ran into some issues.

The first problem I ran into was that most Coldfusion code doesn't have a root element (not to mention it isn't well formed XML in any sense).  You might be thinking "Well CFCs have a root node" and you'd be right. So I tried to transform a really simple CFC and I was somewhat successful.

The only caveat is that the use of a few almost all tags won't work. For example, <cfreturn foo /> isn't proper XML and Saxon complains. So at this point, this is pretty much a worthless exercise (see note above). Either way I ended up with something that did work (in the functional sense, not the useful sense). See the code below if you're interested in what the XSLT looks like.

XSLT Coldfusion

Very Simple CFC

Transformed Output

This entry was posted in Code and tagged , . Bookmark the permalink.

4 Responses to Transforming Coldfusion code with XSLT

  1. Russ says:

    Yeah I have no idea what you would want to do this for. Ideas?

    • Jon Dowdle says:

      I’ve been working with XSLT for the last week so this crazy idea popped into my head. The closest thing to a real-world application that this could be used in would be generating documentation. Fortunately there are much easier ways of doing this (cfcdoc).

  2. Ben Nadel says:

    @Jon,

    I’ve often thought about playing around with this. I never had a real intent, but I thought it would be fun as I love XML. That’s an interesting problem about the parsing. I guess you’d have to make your own custom parser that would be more tag based than pure XML based.

  3. Jon Dowdle says:

    @Ben,

    It was a pretty interesting exercise.

    One could pre-process the CF and make it easier to parse. The simplest way would be to use a few simple regex’s on the input but I feel it would miss many cases. On the more complex side you could write a whole grammar and compile the CF to XML using lex/yacc. (note: I’m not sure if there is an easier way to do this, it is just what we used in school 3 years ago)