Why I love XSLT extension functions

Sigfrid Lundberg's Stuff 2009-07-21

Bookmark and Share

I started learning XSLT eight years ago. At the time, it was a fresh technology, and there was also intense discussions on the extensibility of the language. Since then xslt 1.1 has disappeared down the drains, xpath 2.0 & xquery 1.0 appeared on the market. The former of these has had a slow start. The latter, however, has gained acceptance, however a bit reluctant.

The stuff I want to discuss here is the writing of extension functions. This is regarded as an evil habit in some parts of the XML processing communities, but it is supported by most implementations. It is not to be recommended if you're producing a stand-alone application, but in many cases you're xslt code is an integrated part of an application. Most of my code is of this kind; in particular I'm writing xslt for xalan running in a JAVA environment. Now, why should I refrain from using regex, just because xalan does not support this package? I cannot see why. And nobody would ask me to write ANSI SQL in spite of the fact that we use Oracle.

Now, it has been proven mathematically that xslt is a Turing complete programming language. That means that it is possible to program anything which is possible to program with such a language. I.e., you have theoretically the same possibilities as in JAVA and C++. That doesn't mean that it is practical do do so. It is, in my view, much easier to traverse a XML tree in XSLT than in (say) JAVA, whereas it is much easier to connect to a database or implement a XSLT processor using a conventional programming language.

It is here the XSLT extension functions become really useful. You can pass data from XSLT to your own functions written in your favourite programming lanuguage. I use this for tasks like indexing XML documents. When traversing a document, you select text and pass it to you're function which pass it to your indexing API. One big advantage is that the same software can index heterogenous collections of XML documents just by using different XSLT scripts. They are easy to write and you can a new document type without recompilation of you're indexing software.

This ability of a fullblown functional programming language (mind you, its Turing complete) to interface both with a host program and from scripts inside it call other functions in other languages is powerful. This, in combination that it is really good for what it is designed for has made it an object for extreme affection from me. Besides I'll return to examples of using XSLT for indexing complex XML objects.


Subscribe to Stuff from Sigfrid LundbergSubscribe to my stuff

stuff by category || year


My name is Sigfrid Lundberg. The stuff I publish here may, or may not, be of interest for anyone else.

On this site there is material on photography, music, literature and other stuff I enjoy in life. However, most of it is related to my profession as an Internet programmer and software developer within the area of digital libraries. I have been that at the Royal Danish Library, Copenhagen (Denmark) and, before that, Lund university library (Sweden).

The content here does not reflect the views of my employers. They are now all past employers, since I retired 1 May 2023.

Creative Commons License
This entry (Why I love XSLT extension functions) within Sigfrid Lundberg's Stuff, by Sigfrid Lundberg is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.